同时向数据库输入两个或多个值

时间:2013-03-01 13:39:14

标签: php mysql

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

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

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

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

1 个答案:

答案 0 :(得分: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>

    

要向数据库添加两个或更多值,请使用mysqli在w3schools中使用此示例(注意不推荐使用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