PHP MySQL 无法解释的空值 - 如何在一条记录中插入所有值?

发布于 2024-10-06 03:55:38 字数 2240 浏览 3 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独守阴晴ぅ圆缺 2024-10-13 03:55:38

insert 将一条新记录(行)添加到数据库中 - 您只想执行一次插入语句。

INSERT INTO table1(fname, lname, mail) VALUES ($fname, $lname, $mail);

更新:另外 - 您需要通过转义输入或使用准备好的语句来防止 SQL 注入。

insert adds a new record (row) into the database - you only want to execute an insert statement once.

INSERT INTO table1(fname, lname, mail) VALUES ($fname, $lname, $mail);

UPDATE: Also - you need to protect against sql injection, either by escaping input, or using prepared statements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文