PHPMySQL 3-Day 2 (4 ). 6. before the end of this course, we need to add everything to a program so that it can add, edit, modify, and delete records. This is the first six complete program of all the preceding content.
Before the end of this lesson, we need to add everything to a program so that it can add, edit, modify, and delete records. This is an extension of all the previous content and can also be used as an excellent review method. Look at the following program.
|
$ # @ 60; html $ # @ 62;
$ # @ 60; body $ # @ 62;
$ #@ 60 ;? Php
$ Db = mysql_connect ("localhost", "root ");
Mysql_select_db ("mydb", $ db );
If ($ submit ){
// If there is no ID, we are adding a record; otherwise, we are modifying the record.
If ($ id ){
$ SQL = "UPDATE employees SET first = $ first, last = $ last, Address = $ address, position = $ position WHERE id = $ id ";
} Else {
$ SQL = "INSERT INTO employees (first, last, address, position) VALUES ($ first, $ last, $ address, $ position )";
}
// Issue SQL commands to the database
$ Result = mysql_query ($ SQL );
Echo "record modified successfully! $ # @ 60; p $ # @ 62 ;";
} Elseif ($ delete ){
// Delete a record
$ SQL = "DELETE FROM employees WHERE id = $ id ";
$ Result = mysql_query ($ SQL );
Echo "record deleted successfully! $ # @ 60; p $ # @ 62 ;";
} Else {
// If we have not pressed the submit button, execute the following program
If (! $ Id ){
// If the status is not modified, the employee list is displayed.
$ Result = mysql_query ("SELECT * FROM employees", $ db );
While ($ myrow = mysql_fetch_array ($ result )){
Printf ("$ # @ 60; a f =" % s? Id = % s "$ # @ 62; % s $ # @ 60;/a $ # @ 62 ;", $ PATH_INFO, $ myrow ["id"], $ myrow ["first"], $ myrow ["last"]);
Printf ("$ # @ 60; a href =" % s? Id = % s & delete = yes "$ # @ 62; (DELETE) $ # @ 60;/a $ # @ 62; $ # @ 60; Br $ # @ 62; ", $ PATH_INFO, $ myrow [" id "]);
}
}
? $ #@ 62;
$ # @ 60; P $ # @ 62;
$ #@ 60; a href = "$ # @ 60 ;? Php echo $ PATH_INFO? $ # @ 62; "$ # @ 62; add a record $ # @ 60;/a $ # @ 62;
$ # @ 60; P $ # @ 62;
$ #@ 60; form method = "post" action = "$ # @ 60 ;? Php echo $ PATH_INFO? $ # @ 62; "$ # @ 62;
$ #@ 60 ;? Php
If ($ id ){
// We are editing the modification status. for some reasons, select a record.
$ SQL = "SELECT * FROM employees WHERE id = $ id ";
$ Result = mysql_query ($ SQL );
$ Myrow = mysql_fetch_array ($ result );
$ Id = $ myrow ["id"];
$ First = $ myrow ["first"];
$ Last = $ myrow ["last"];
$ Address = $ myrow ["address"];
$ Position = $ myrow ["position"];
// Display the id for editing and modification.
? $ #@ 62;
$ #@ 60; input type = hidden name = "id" value = "$ # @ 60 ;? Php echo $ id? $ # @ 62; "$ # @ 62;
$ #@ 60 ;? Php
}
? $ #@ 62;
Name: $ # @ 60; input type = "Text" name = "first" value = "$ # @ 60 ;? Php echo $ first? $ # @ 62; "$ # @ 62; $ # @ 60; br $ # @ 62;
Last name: $ # @ 60; input type = "Text" name = "last" value = "$ # @ 60 ;? Php echo $ last? $ # @ 62; "$ # @ 62; $ # @ 60; br $ # @ 62;
Address: $ # @ 60; input type = "Text" name = "address" value = "$ # @ 60 ;? Php echo $ address? $ # @ 62; "$ # @ 62; $ # @ 60; br $ # @ 62;
Position: $ # @ 60; input type = "Text" name = "position" value = "$ # @ 60 ;? Php echo $ position? $ # @ 62; "$ # @ 62; $ # @ 60; br $ # @ 62;
$ # @ 60; input type = "Submit" name = "submit" value = "input information" $ # @ 62;
$ # @ 60;/form $ # @ 62;
$ #@ 60 ;? Php
}
? $ #@ 62;
$ # @ 60;/body $ # @ 62;
$ # @ 60; '/html $ # @ 62;
|
This process looks complicated, but it is not actually difficult. The program consists of three parts. The first if () statement checks whether we have pressed the "input information" data submit button. If yes, check whether $ id exists. If it does not exist, we are adding the record status. Otherwise, we are modifying the record status.
Next, check whether the variable $ delete exists. If yes, we want to delete the record. Note that the first if () statement checks the variables sent using the POST method, and this time we check the variables passed in the GET method.
Finally, the default action of the program is to display the employee list and table. Similarly, we need to check whether the variable $ id exists. If yes, we can retrieve the corresponding records based on their values. Otherwise, an empty table is displayed.
Now, we have put everything we have learned in a program. We used the while () loop, the if () statement, and executed all the basic SQL operations-SELECT, INSERT, UPDATE, and DELETE. In addition, we also know how to transmit information between different webpages through URL and table input.
In the third lesson, we need to learn how to add intelligent processing capabilities for webpages.
Before the end of this lesson, we need to add everything to a program so that it can add, edit, modify, and delete records. This is one of all the previous contents...