0

我是 PHP 新手,我这样做对吗?下面代码的功能将同时将 2 个值放入数据库中。一旦我在 2 个文本框中输入值,只有第一个文本框中的值会存储到数据库中。如何使用一种形式将这两个值存储到数据库中?

添加.php

    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
        }

    mysql_select_db("my_database", $con);


    mysql_close($con);
    ?> 

    <html>
    <body>

    <form action="insert.php" method="post" name="form1">
    Firstname: <input type="text" name="firstname" />
    Firstname1: <input type="text" name="firstname" />

    Age: <input type="text" name="age" />



    <input type="submit" name="form1" />
    </form>



    </body>
    </html> 

插入.php

        <html>
     <head>
      <title>Record Added</title>
      <meta http-equiv="refresh" content="0;url=select.php">
     </head>
     <body>
     <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

    mysql_select_db("my_database", $con);

    $sql="INSERT INTO biodata (FirstName)
    VALUES
    ('$_POST[firstname]')";

    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "1 record added";

    mysql_close($con);
    ?> 

    <a href="select.php"> see</a>

     </body>
    </html>

选择.php

    <?php
    $dbHost = 'localhost';
    $dbUser = 'root';
    $dbPass = '';
    $dbName = 'my_database';

    $dbConn = mysql_connect ($dbHost,$dbUser, $dbPass) or die ('MySQL connect failed. ' . mysql_error());
    mysql_select_db($dbName) or die('Cannot select database. ' . mysql_error());

    ?>

    <html>
    <head>
    <title>Display</title>
    </head>
    <table align="center" border=1>
    <tr>
    <th> First Name</th>
    <th>Last Name </th>
    <th> Age </th>
    </tr>

    <?php
    $query = "SELECT * FROM biodata";
    $result = mysql_query($query);
    while ($row = mysql_fetch_assoc($result)){
    ?>
    <tr><td><?php echo $row['FirstName'];?></td>

    </tr>
    <?php }?>
    </table>
    </body>
    </html>
4

1 回答 1

1

[更新]试试这个:

将表单中的值作为数组发送,以访问第二个输入。否则,您的记录将被覆盖。除了它们形成一个数组之外,您实际上不能有两个具有相同名称的表单字段。这应该是您的最佳选择。在这里,我们将两个值存储在同一个表中的两列中。

你的表格

<form action="add.php" method="post" name="verify">
<font color="#FFFF00">Name:</font> <input type="text" name="FistName[0]" />
<font color="#FFFF00">Name:</font> <input type="text" name="FirstName[1]"/>

<input type="submit" value="add" name="verify"/>

使用它将文本框 2 添加到数据库中

<?php
$names = $_POST["FirstName"];
$FirstName_1 = mysql_real_escape_string($names[0]);
$FirstName_2 = mysql_real_escape_string($names[1]);

$query = "INSERT INTO biodata (FirstName_1, FirstName_2)
VALUES ('" . $FirstName_1 . "', '" . $FirstName_2 . "') ";
$result = mysql_query($query);

?>

使用它从数据库中显示

<?php
$query = "SELECT * FROM biodata";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)){
?>
<tr><td><?php echo $row["FirstName_1"];?></td>
</tr>

要将两个或多个值添加到数据库,请按照 w3schools 中的此示例使用 mysqli(注意 mysql 已弃用)。

<?php
//Filename: insert.php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error());
  }
echo "1 record added";

mysqli_close($con);
?>

你的表格:

<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>

在这里运行一个简单的课程:

http://www.w3schools.com/php/php_mysql_insert.asp

于 2013-03-01T14:55:22.033 回答