PHP MySQL 无法解释的空值 - 如何在一条记录中插入所有值?
我有这个 HTML 表单页面:
form1.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"
>
<html lang="en">
<head>
<title>Insert Your Details</title>
</head>
<body>
<h3> Insert Your Name</h3>
<form action="form1.php" method="post">
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
E-mail: <input type="text" name="mail"><br>
<input type="Submit" value="Submit" name="Submit">
</form>
</body>
</html>
转发到这个 PHP 脚本来处理表单:
form1.php
<?php
$connection = mysql_connect("localhost","root","")
or die ("Couldn't Connect To Server");
$db = mysql_select_db("db1", $connection)
or die ("Couldn't Select Database");
$query = "CREATE TABLE IF NOT EXISTS table1 (Name VARCHAR(20))";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
$query = "INSERT INTO table1 (fname) VALUES ('".$_POST[fname]."')";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
$query = "INSERT INTO table1 (lname) VALUES ('".$_POST[lname]."')";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
$query = "INSERT INTO table1 (mail) VALUES ('".$_POST[mail]."')";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
$query = "SELECT * FROM table1";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
echo "<TABLE BORDER = '1'>";
echo "<TR>";
echo "<TH>First Name</TH>";
echo "</TR>";
while ($row = mysql_fetch_array($result))
{
echo "<TR>";
echo "<TD>", $row['fname'], "</TD><TD>",
$row['lname'], "</TD><TD>",
$row['mail'], "</TD>";
echo "</TR>";
}
echo "</TABLE>";
mysql_close($connection);
?>
现在,由于某种原因,当我插入这些值时:
First Name: Wide
Last Name: Blade
我在 MySQL 表中得到很多 NULL 值。
所以我的问题是:如何将所有这些值插入到同一条记录中,这样我就不会在表中得到这些 NULL 记录?
I have this HTML form page:
form1.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"
>
<html lang="en">
<head>
<title>Insert Your Details</title>
</head>
<body>
<h3> Insert Your Name</h3>
<form action="form1.php" method="post">
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
E-mail: <input type="text" name="mail"><br>
<input type="Submit" value="Submit" name="Submit">
</form>
</body>
</html>
That forwards to this PHP script to handle the form:
form1.php
<?php
$connection = mysql_connect("localhost","root","")
or die ("Couldn't Connect To Server");
$db = mysql_select_db("db1", $connection)
or die ("Couldn't Select Database");
$query = "CREATE TABLE IF NOT EXISTS table1 (Name VARCHAR(20))";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
$query = "INSERT INTO table1 (fname) VALUES ('".$_POST[fname]."')";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
$query = "INSERT INTO table1 (lname) VALUES ('".$_POST[lname]."')";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
$query = "INSERT INTO table1 (mail) VALUES ('".$_POST[mail]."')";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
$query = "SELECT * FROM table1";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
echo "<TABLE BORDER = '1'>";
echo "<TR>";
echo "<TH>First Name</TH>";
echo "</TR>";
while ($row = mysql_fetch_array($result))
{
echo "<TR>";
echo "<TD>", $row['fname'], "</TD><TD>",
$row['lname'], "</TD><TD>",
$row['mail'], "</TD>";
echo "</TR>";
}
echo "</TABLE>";
mysql_close($connection);
?>
Now, for some reason, when I insert these values:
First Name: Wide
Last Name: Blade
I get lots of NULL values in the MySQL Table.
So my question is: How do I insert all of these values in the same record, so I don't get these NULL records in the table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
insert 将一条新记录(行)添加到数据库中 - 您只想执行一次插入语句。
更新:另外 - 您需要通过转义输入或使用准备好的语句来防止 SQL 注入。
insert adds a new record (row) into the database - you only want to execute an insert statement once.
UPDATE: Also - you need to protect against sql injection, either by escaping input, or using prepared statements.