& Nbsp; 5. modify data in a tutorial, I put all the SQL statements to be executed into a variable ($ SQL), and then use mysql_query () to execute database queries. This is useful during debugging. If something goes wrong with the program, you can change SQL Syntax V and data at any time.
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.