Vi. Complete Procedures
Before the end of this lesson, we have to add everything to a program that has the ability to add, edit, and delete records. This is an extension of everything in front of you, as well as an excellent way to review it. Check out the program below.
|
$#@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, then we are increasing the 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) ";
}
Issuing 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 yet pressed the submit button, then perform this part of the 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%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 modified state, because some 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 ID for users to edit and modify
? $#@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;
Surname: $#@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 program looks complicated, but it's not really difficult. There are three main parts of the program. The first if () statement checks to see if we have pressed the data submit button for the "Input information". If so, the program then checks to see if the $id exists. If it does not exist, then we are increasing the record state, otherwise we are modifying the record state.
Next we check whether the variable $delete exists. If there is, we are going to delete the record. Note that the first if () statement examines the variables sent with the Post method, and this time we examine the variables passed in the Get method.
Finally, the default action of the program is to display the Employee list and table. Again, we want to check if the variable $id exists. If it exists, we retrieve the corresponding record according to its value. Otherwise, we will display an empty table.
Now, we've put everything we've learned into a program. We used the while () loop, used the IF () statement, and performed all the basic SQL operations-SELECT, INSERT, update, and delete. In addition, we know how to communicate information to each other through URLs and table inputs between different Web pages.
In the third lesson, we want to learn how to add intelligent processing power to the Web page.
http://www.bkjia.com/PHPjc/532611.html www.bkjia.com true http://www.bkjia.com/PHPjc/532611.html techarticle Vi. complete procedures before the end of this lesson, we will add everything to a program that has the ability to add, edit, and delete records. This is one of the previous contents ...