PHP and MySQL Delete specified numbers of employee information

Source: Internet
Author: User
Tags mysql delete php and php and mysql php script mysql database

Example of a DELETE statement:

The code is as follows Copy Code

Delete from friends where user_name = ' simaopig ';

Grammatical 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 records in the table and deletes them immediately

1. Delete the employee information for the specified number

The code is as follows Copy Code

<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 (!empty ($_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 Technologies
The key point of this example is the flexible application of the while () loop statement. Executes the while () loop statement, performs a delete operation on the cycle weight, and completes the cyclic deletion of the data according to the submitted parameter values.
Design process
(1) Create PHP script file. First, connect the MySQL database server, connect the database named Db_while in MySQL, and set the encoding format of the page. Then, define the loop variable $a and $b, set the loop condition in the 1th while loop to the $b< $a, and then reuse the while statement to iterate through the data in the datasheet and perform the deletion of the data, with the following code:

The code is as follows Copy Code

<?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 MR266 folder, named index.php.

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.