Php+mysql the method of deleting a specified number of employee information _php tips

Source: Internet
Author: User
Tags php script

The example in this article describes how php+mysql deletes a specified number of employee information. Share to everyone for your reference. The specific analysis is as follows:

Use PHP to submit the employee number to MySQL, and then by MySQL to accept the data and use delete for data deletion operations, here to share a few concrete examples.

Example of a DELETE statement:

Copy Code code as follows:
Delete from friends where user_name = ' simaopig ';

Syntax structure, we can see that, like the update syntax, we can omit the WHERE clause, but this is a very dangerous behavior, because if you do not specify a WHERE clause, delete deletes all the records in the table and deletes them immediately.

1. Delete the specified number of employee information, the code is as follows:

Copy Code code as follows:
<body>
<center> Employee Information Table </center>
<table border= "1" align= ' center ' >
<thead><tr><th> number </th><th> name </th><th> age </th></tr></ Thead>
<tbody>
<?php
Header ("content-type:text/html; Charset=utf-8 ");
$_session[' arr ']=array (' 1 ' =>array (' name ' => ' John ', ' age ' =>18), ' 2 ' =>array (' name ' => ' Dick ', ' age ' =>19) , ' 3 ' =>array (' name ' => ' Harry ', ' age ' =>20));
if (!emptyempty ($_post[' num ')) {
unset ($_session[' arr '][$_post[' num ']); Delete Employee information for the specified ID
}
if (Isset ($_session[' arr ')) {
foreach ($_session[' arr '] as $k => $v) {
echo "<tr><td>". ($k). " </td><td> ". $v [' name ']." </td><td> ". $v [' age ']." </td></tr> ";
}
}

?>
</tbody>
</table>

<form method= ' post ' action= ' "><br>
<center>
<b> Enter employee number:</b>
<input type= "text" name= "num" size= ' 5 ' ><br>
<input type= ' submit ' value= ' delete ' name= ' Sub ' class= ' btn ' >
</center>
</form>
</body>

2. Employee Information Batch Deletion example

Key technology: The key point of this example is the flexible application of the while () loop statement, executing the while () loop statement, performing the delete operation on the cycle weight, and completing the cyclic deletion of the data according to the submitted parameter values.

Design process:

(1) Create PHP script files, first, connect MySQL database server, connect MySQL name db_while database, and set the page encoding format, and then, define the loop variable variable $a and $b, set the loop condition in the 1th while loop to $b< $a , and then use the while statement to iterate through the data in the datasheet and perform the deletion of the data, with the following code:

Copy Code code as follows:
<?php
if ($_post[sub]) {//Get parameters by POST method
$conn =mysql_connect ("localhost", "root", "111"); Connecting to the MySQL database
mysql_select_db ("Db_while", $conn); Connecting to a database
mysql_query ("SET NAMES GBK");//define Encoding format
$a =$_post[te];//receive text box parameters
Defining Variables $b =0;//
while ($b < $a) {//while loop
$rs =mysql_query ("SELECT * from Tb_while")//Execute query operation
while ($rst =mysql_fetch_array ($rs)) {//Save query results in array
$sql = "Delete * from Tb_while where id= $b"; SQL statement
mysql_query ($sql);//Perform the delete operation
echo "Name is". $rst [name]. Employee has been deleted <br>//output deleted employee name
$b + +
}
}
}
?>

(2) Store the file in the Mr 2 66 folder, named index.php.

I hope this article will help you with the Php+mysql program design.

Related Article

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.