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