Php/mysql 3rd Pass (6)

Source: Internet
Author: User
Tags printf

Vi. Complete Procedures

Before the end of the lesson, we'll add everything to a program that has the ability to add, edit, and delete records. This is an extension of all the previous content, and can also be an excellent review method. Look at the following procedure.

<body>
<?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 ');
}
To issue SQL commands to the database
$result = mysql_query ($sql);
echo "Record modification successful! <p> ";
} elseif ($delete) {
Delete a record
$sql = "DELETE from Employees WHERE id= $id";
$result = mysql_query ($sql);
echo "Record deletion succeeded!" <p> ";
} else {
If we haven't pressed the submit button, then execute the following part of the program
if (! $id) {
If the status is not modified, the list of employees is displayed
$result = mysql_query ("SELECT * FROM Employees", $DB);
while ($myrow = Mysql_fetch_array ($result)) {
printf ("<a href=\"%s?id=%s\ ">%s%s</a> \ n",
$PATH _info, $myrow ["id"], $myrow ["a"], $myrow ["last"]);
printf ("<a href=\"%s?id=%s&delete=yes\ "> (delete) </a><
Br> ", $PATH _info, $myrow [" id "]);
}
}
?>
<P>
<a href= "<?php echo $PATH _info?>" >add a record</a>
<P>
<form method= "POST" action= "<?php echo $PATH _info?>" >
<?php
if ($id) {
We are editing the change status, because we select a record
$sql = "SELECT * FROM Employees WHERE id= $id";
$result = mysql_query ($sql);
$myrow = Mysql_fetch_array ($result);
$id = $myrow ["id"];
$first = $myrow ["a"];
$last = $myrow ["Last"];
$address = $myrow ["Address"];
$position = $myrow ["position"];
Display IDs for users to edit and modify
?>
<input type=hidden name= "id" value= "<?php echo $id?>" >
<?php
}
?>
Name: <input type= "Text" name= "one" value= "<?php echo $first?>" ><br>
Surname: <input type= "Text" Name= "Last" value= "<?php echo $last?>" ><br>
Address: <input type= "Text" name= "adress" value= "<?php echo $address?>" ><br>
Position: <input type= "Text" name= "position" value= "<?php echo $position?>" ><br>
<input type= "Submit" name= "submit" value= "Input Info" >
</form>
<?php
}
?>
</body>

The program looks complicated, but it's not really difficult. There are three main parts of the program. The first if () statement checks whether we have pressed the "Enter information" data submission button. If so, the program checks to see if the $id exists. If it does not exist, then we are increasing the state of the record, 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 a variable that is sent with the Post method, and this time we examine the variable passed in the Get method.

Finally, the default action of the program is to display a list of employees and tables. Again, we want to check whether the variable $id exists. If it exists, we will retrieve the corresponding record according to its value. Otherwise, we will display an empty table.

Now, we've put everything we've learned into one 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 also know how to transfer information between different Web pages through URL and form input.

In the third lesson, we want to learn how to increase the intelligent processing capacity of the Web page.

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.