PHP mysq Data Edit Update instance

Source: Internet
Author: User
The code is as follows Copy Code

$db = MySQL tutorial _connect ("localhost", "phpdb", "phpdb");
mysql_select_db ("Test", $db);

If the submit button is submitted
if ($submit) {
If there is no ID, the record is incremented, otherwise the record is modified
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 a database tutorial
$result = mysql_query ($sql);
echo "Record modification successful! <> ";
echo "<a href= ' $php _self ' > Return </a>";
}
ElseIf ($delete) {
Delete a record
$sql = "Delete from employees where id= $id";
$result = mysql_query ($sql);
echo "Record deletion succeeded!" <> ";
echo "<a href= ' $php _self ' > Return </a>";
}
else {
If the submit button has not been pressed, 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>",
$php _self, $myrow ["id"], $myrow ["a"], $myrow ["last"]);
printf ("<a href="%s?id=%s&delete=yes "> (delete) </a><br>", $php _self, $myrow ["id"]);
}
}
?>
<a href= "<?php echo $php _self?>" > Return </a>
<form method= "POST" action= "<?php echo $php _self?>" >
<?php
if ($id) {
Is in edit modification state, 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?>" >
Surname: <input type= "text" Name= "last" value= "<?php echo $last?>" >
<br>
Address: <input type= "text" name= "adress" value= "<?php echo $address?>" >
Position: <input type= "text" name= "position" value= "<?php echo $position?>" >
<br>
<input type= "Submit" name= "submit" value= "Input Info" >
</form>
<?php
}
?>
</body>

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.