Php/mysql 3rd-The next day (ii)

Source: Internet
Author: User
Tags mysql variables
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.

  $#@60;html$#@62;

$#@60;body$#@62;

$#@60;form method= "POST" action= "$#@60;? PHP echo $PATH _info?$#@62; " $#@62;

Name: $#@60;input type= "Text" name= "one" $#@62;$#@60;br$#@62;

Surname: $#@60;input type= "Text" Name= "last" $#@62;$#@60;br$#@62;

Address: $#@60;input type= "Text" name= "addresses" $#@62;$#@60;BR$#@62;

Position: $#@60;input type= "Text" name= "position" $#@62;$#@60;br$#@62;

$#@60;input type= "Submit" name= "submit" value= "input information" $#@62;

$#@60;/form$#@62;

$#@60;/body$#@62;

$#@60;/html$#@62;

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.

$#@60;html$#@62;

$#@60;body$#@62;

$#@60;? PHP

if ($submit) {


//Process table input

while ($name, $value) = each ($HTTP _post_vars)) {

echo "$name = $value $#@60;br$#@62;\n";

}

} else{


//Show Table

$#@62;

$#@60;form method= "POST" action= "$#@60;? PHP echo $PATH _info?$#@62; " $#@62;

Name: $#@60;input type= "Text" name= "one" $#@62;$#@60;br$#@62;

Surname: $#@60;input type= "Text" Name= "last" $#@62;$#@60;br$#@62;

Address: $#@60;input type= "Text" name= "addresses" $#@62;$#@60;BR$#@62;

Position: $#@60;input type= "Text" name= "position" $#@62;$#@60;br$#@62;

$#@60;input type= "Submit" name= "submit" value= "input info" $#@62;

$#@60;/form$#@62;

$#@60;? PHP

}//End If,if

? $#@62;

$#@60;/body$#@62;

$#@60;/html$#@62;

Now that the program is working properly, we can now take the contents of the form and send them to the database.
  $#@60;html$#@62;

$#@60;body$#@62;

$#@60;? 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

? $#@62;

$#@60;form method= "POST" action= "$#@60;? PHP echo $PATH _info?$#@62; " $#@62;

Name: $#@60;input type= "Text" name= "one" $#@62;$#@60;br$#@62;

Surname: $#@60;input type= "Text" Name= "last" $#@62;$#@60;br$#@62;

Address: $#@60;input type= "Text" name= "addresses" $#@62;$#@60;BR$#@62;

Position: $#@60;input type= "Text" name= "position" $#@62;$#@60;br$#@62;

$#@60;input type= "Submit" name= "submit" value= "input information" $#@62;

$#@60;/form$#@62;

$#@60;? Php

}//End If,if


? $#@62;

$#@60;/body$#@62;

$#@60;/html$#@62;

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? Don't worry. Let's take it one step at a step.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.