开发者

PHP MySQL Unexplained Null Values-How To Insert all Values In one Record?

开发者 https://www.devze.com 2023-01-28 19:34 出处:网络
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\"

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">
    Firs开发者_JAVA技巧t 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消