zoukankan      html  css  js  c++  java
  • 数据库中插入记录

    把一张表中的数据插入数据库中

    现在,我们将建立一个HTML表单;通过它我们可以向“Person”表中加入新的记录。

    下面演示这个HTML表单:

    <html> <body><form action="insert.php" method="post"> Firstname: <input type="text" name="firstname" /> Lastname: <input type="text" name="lastname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form></body> </html>

    在上述案例中,当一个用户点击HTML表单中的“提交submit”按钮后,表单中的数据会发送到“insert.php”。“insert.php”文件与数据库建立连接,并通过PHP $_POST变量获取表单中的数据;此时,mysql_query()函数执行“INSERT INTO”语句,这样,一条新的记录就被添加到数据库的表单当中了。

    下面试“insert.php”页面的代码:

    <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con)   {   die('Could not connect: ' . mysql_error());   }mysql_select_db("my_db", $con);$sql="INSERT INTO person (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";if (!mysql_query($sql,$con))   {   die('Error: ' . mysql_error());   } echo "1 record added";mysql_close($con) ?>

  • 相关阅读:
    AT2165 Median Pyramid Hard
    AT2160 へんなコンパス / Manhattan Compass
    bzoj2863:愤怒的元首
    bzoj5336:[TJOI2018]party
    [luoguP3768]简单的数学题
    bzoj1831:[AHOI2008]逆序对
    bzoj5492:[Hnoi2019]校园旅行
    HNOI2019游记
    HDU 1102 Constructing Roads(kruskal)
    HDU 1059 Dividing
  • 原文地址:https://www.cnblogs.com/teyues/p/5941047.html
Copyright © 2011-2022 走看看