Php/mysql 3rd Pass-day two (ii) _php tutorial

Source: Internet
Author: User
Iv. sending data to the server

Now we don't have much difficulty reading the data from the database. But how do you send data to the database in turn? Actually 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= "first" $#@62;$#@60;br$#@62;

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

Address: $#@60;input type= "Text" name= "Address" $#@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, so it is more intuitive to do so, so that you will understand the code later.

Also note that I added the name attribute to the Submit button. This way I can test whether the $submit variable exists in the program. So, when the page is called again, I know if the table was filled in when the page was called.

I should point out that you do not have to write the contents of the above page into a PHP program, and then return to the calling program itself. You can completely separate the page that displays the form and the program that processes the table on two pages, three pages or even more pages. Putting in a file just makes the content more compact.

Well, let's add some code to check what the user has entered in the table. I'll show all the query parameter variables with $http_post_vars, just to prove that PHP actually passes all the variables to the program. This method is a 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) {


Working with Table input

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

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

}

} else{


Show Table

? $#@62;

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

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

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

Address: $#@60;input type= "Text" name= "Address" $#@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;

This news a total of 2 pages, currently on the 1th page 1 2


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

$#@60;body$#@62;

$#@60;? Php

if ($submit) {


Working with Table 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. ";

} else{


Show Table Contents

? $#@62;

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

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

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

Address: $#@60;input type= "Text" name= "Address" $#@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's a lot of good work to do. What if a user doesn't fill out a column? What if I fill in the text where I need to fill in the numbers? Or what if you fill in the wrong? Don't worry. Let's take one step at a pace.

This news a total of 2 pages, currently on the 2nd Page 1 2


http://www.bkjia.com/PHPjc/532613.html www.bkjia.com true http://www.bkjia.com/PHPjc/532613.html techarticle Iv. sending 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? Actually this is not a problem with PHP. The first ...

  • 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.