PHP/MySQL 3-day 2 (3)

Source: Internet
Author: User

5. modify data

In this tutorial, I put the SQL statement to be executed into a variable ($ SQL) before using mysql_query () to execute database queries. This is useful during debugging. If something goes wrong with the program, you can display the content of the SQL statement at any time and check for syntax errors.

We have learned how to insert data into the database. Now let's learn how to modify the existing records in the database. Data editing consists of two parts: data display and table input to return data to the database. We have already mentioned these two parts. However, data editing is a little different. We must first display the relevant data in the table.

First, let's look back at the program code of Lesson 1 and display the employee name on the webpage. But this time, we will display the data in the table. The program looks like the following:

 

  $ # @ 60; html $ # @ 62;

$ # @ 60; body $ # @ 62;

$ #@ 60 ;? Php

$ Db = mysql_connect ("localhost", "root ");

Mysql_select_db ("mydb", $ db );

If ($ id ){


// Query the database

$ SQL = "SELECT * FROM employees WHERE id = $ id ";

$ Result = mysql_query ($ SQL );

$ Myrow = mysql_fetch_array ($ result );

? $ #@ 62;

$ #@ 60; form method = "post" action = "$ # @ 60 ;? Php echo $ PATH_INFO? $ # @ 62; "$ # @ 62;

$ #@ 60; input type = hidden name = "id" value = "$ # @ 60 ;? Php echo $ myrow ["id"]? $ # @ 62; "$ # @ 62;

Name: $ # @ 60; input type = "Text" name = "first" value = "$ # @ 60 ;? Php echo
$ Myrow ["first"]? $ # @ 62; "$ # @ 62; $ # @ 60; br $ # @ 62;

Last name: $ # @ 60; input type = "Text" name = "last" value = "$ # @ 60 ;? Php echo
$ Myrow ["last"]? $ # @ 62; "$ # @ 62; $ # @ 60; br $ # @ 62;

Address: $ # @ 60; input type = "Text" name = "address" value = "$ # @ 60 ;? Php echo
$ Myrow ["address"]? $ # @ 62; "$ # @ 62; $ # @ 60; br $ # @ 62;

Position: $ # @ 60; input type = "Text" name = "position" value = "$ # @ 60 ;? Php echo
$ Myrow ["position"]? $ # @ 62; "$ # @ 62; $ # @ 60; br $ # @ 62;

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

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

$ #@ 60 ;? Php

} Else {


// Display the employee list

$ Result = mysql_query ("SELECT * FROM employees", $ db );

While ($ myrow = mysql_fetch_array ($ result )){

Printf ("$ # @ 60; a href =" % s? Id = % s "$ # @ 62; % s $ # @ 60;/a $ # @ 62; $ # @ 60; br $ # @ 62 ;", $ PATH_INFO,
$ Myrow ["id"], $ myrow ["first"], $ myrow ["last"]);

}

}

? $ #@ 62;

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

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

We just wrote the field content to the value attribute in the corresponding table element. This is simple. Let's proceed further so that the program can write the modified content back to the database. Similarly, we can use the Submit button to determine whether to process table input content. Note that the SQL statements we use are slightly different.

There are two pages in this news. Currently, there are two pages in page 1st.


  $ # @ 60; html $ # @ 62;

$ # @ 60; body $ # @ 62;

$ #@ 60 ;? Php

$ Db = mysql_connect ("localhost", "root ");

Mysql_select_db ("mydb", $ db );

If ($ id ){

If ($ submit ){

$ SQL = "UPDATE employees SET first = $ first, last = $ last,
Address = $ address, position = $ position WHERE id = $ id ";

$ Result = mysql_query ($ SQL );


Echo "Thank you! Data changed ";

} Else {


// Query the database

$ SQL = "SELECT * FROM employees WHERE id = $ id ";

$ Result = mysql_query ($ SQL );

$ Myrow = mysql_fetch_array ($ result );

? $ #@ 62;

$ #@ 60; form method = "post" action = "$ # @ 60 ;? Php echo $ PATH_INFO? $ # @ 62; "$ # @ 62;

$ #@ 60; input type = hidden name = "id" value = "$ # @ 60 ;? Php echo $ myrow ["id"]? $ # @ 62; "$ # @ 62;


Name: $ # @ 60; input type = "Text" name = "first" value = "$ # @ 60 ;? Php
Echo $ myrow ["first"]? $ # @ 62; "$ # @ 62; $ # @ 60; br $ # @ 62;


Last name: $ # @ 60; input type = "Text" name = "last" value = "$ # @ 60 ;? Php echo
$ Myrow ["last"]? $ # @ 62; "$ # @ 62; $ # @ 60; br $ # @ 62;


Address: $ # @ 60; input type = "Text" name = "address" value = "$ # @ 60 ;? Php echo
$ Myrow ["address"]? $ # @ 62; "$ # @ 62; $ # @ 60; br $ # @ 62;


Position: $ # @ 60; input type = "Text" name = "position" value = "$ # @ 60 ;? Php echo
$ Myrow ["position"]? $ # @ 62; "$ # @ 62; $ # @ 60; br $ # @ 62;


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

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

$ #@ 60 ;? Php

}

} Else {


// Display the employee list

$ Result = mysql_query ("SELECT * FROM employees", $ db );

While ($ myrow = mysql_fetch_array ($ result )){

Printf ("$ # @ 60; a href =" % s? Id = % s "$ # @ 62; % s $ # @ 60;/a $ # @ 62; $ # @ 60; br $ # @ 62 ;", $ PATH_INFO,
$ Myrow ["id"], $ myrow ["first"], $ myrow ["last"]);

}

}

? $ #@ 62;

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

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

That's it. This program already contains most of the features we have learned. You can also see that an if () Statement is added to an if () condition discriminant statement to check multiple conditions.

Next, we will add everything together and write a good program.

There are two pages in this news. Currently, there are two pages in page 2nd.


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.