Four, send data to the server
Now we don't have much difficulty reading data from the database. But how do you send data to the database in turn? In fact, this is not a problem with PHP.
First, we create a Web page with a simple table.
<body>
<form method="post" action="<?php echo $PATH_INFO?>">
名:<input type="Text" name="first"><br>
姓:<input type="Text" name="last"><br>
住址:<input type="Text" name="address"><br>
职位:<input type="Text" name="position"><br>
<input type="Submit" name="submit" value="输入信息">
</form>
</body>
Also pay attention to the use of $path_info. As I mentioned in the first lesson, you can use PHP anywhere in the HTML code. You will also notice that each element in the table corresponds to a field in the database. This correspondence is not necessary, it is only a little more intuitive to make it easier for you to understand the code later.
Also note that I added the name attribute to the Submit button. So I can test the existence of $submit variables in the program. Then, when the page is called again, I know whether the page has been filled in with the form when it is called.
I should point out that you do not have to write the contents of the above page into a PHP program, and then come back and call the program itself. You can put the page that shows the table and the process of processing the table separately on two pages, three pages or even more pages, whichever you want. Putting it in a file can only make the content more compact.
Well, now we're going to add some code to check what the user has entered into the table. I'll show all the query parameter variables with $http_post_vars, just to show that PHP does pass all variables to the program. This method is a very useful debugging tool. If you want to see all the variables, you can use $globals.
<body>
<?php
if ($submit) {
Working with form input
while (the list ($name, $value) = each ($HTTP _post_vars)) {
echo "$name = $value <br>\n";
}
} else{
Show Table
?>
<form method= "POST" action= "<?php echo $PATH _info?>" >
Name: <input type= "Text" name= "one" ><br>
Surname: <input type= "Text" Name= "Last" ><br>
Address: <input type= "Text" name= "adress" ><br>
Position: <input type= "Text" name= "position" ><br>
<input type= "Submit" name= "submit" value= "Input Info" >
</form>
<?php
}//End If,if
?>
</body>
Now that the program is working properly, we can now take the contents of the form and send them to the database.
<body>
<?php
if ($submit) {
Working with form input
$db = mysql_connect ("localhost", "root");
mysql_select_db ("MyDB", $db);
$sql = "INSERT into employees (first,last,address,position)
VALUES (' $first ', ' $last ', ' $address ', ' $position ');
$result = mysql_query ($sql);
echo "Thank you! Information entered.\n ";
} else{
Show Table Contents
?>
<form method= "POST" action= "<?php echo $PATH _info?>" >
Name: <input type= "Text" name= "one" ><br>
Surname: <input type= "Text" Name= "Last" ><br>
Address: <input type= "Text" name= "adress" ><br>
Position: <input type= "Text" name= "position" ><br>
<input type= "Submit" name= "submit" value= "Input Info" >
</form>
<?php
}//End If,if
?>
</body>
You have now inserted data into the database. But there are still a lot of good work to do. What if a user doesn't fill out a column? What to do if you fill in a number where you need to fill in the text? Or fill in the wrong?