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.