Php + mysql: how to delete employee information with specified number

Source: Internet
Author: User

Php + mysql: how to delete employee information with specified number

This article describes how to delete the employee information of a specified number in php + mysql. It involves mysql database operation skills and has some reference value. For more information, see

 

 

This example describes how to delete employee information of a specified number in php + mysql. Share it with you for your reference. The specific analysis is as follows:

Submit the employee ID to mysql using php, and then mysql accepts the data and uses delete to delete the data. Here we will share with you a few examples.

Example of a delete statement:

The Code is as follows:

Delete from friends where user_name = 'simaopig ';

 

In the 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 the where clause, delete deletes all records in the table and deletes them immediately.

1. Delete the employee information of the specified number. The Code is as follows:

The Code is as follows:

<Html>
<Head>
</Head>
<Body>
<Center> employee information table </center>
<Table border = "1" align = 'center'>
<Thead> <tr> <th> NO. </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' => 'zhang san', 'age' => 18 ), '2' => array ('name' => 'Li si', 'age' => 19), '3' => array ('name' => 'wang 5 ', 'age' => 20 ));
If (! Emptyempty ($ _ POST ['num']) {
Unset ($ _ SESSION ['arr'] [$ _ POST ['num']); // deletes the employee information of a 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 ID: </B>
<Input type = "text" name = "num" size = '5'> <br>
<Input type = 'submit 'value = 'delete' name = 'sub' class = 'btn'>
</Center>
</Form>
</Body>
</Html>


2. Example of batch deletion of employee information

 

Key Technology: The key point of this instance is the flexible use of the while () loop statement. Execute the while () loop statement based on the submitted parameter value and delete the loop weight, the data is deleted cyclically.

Design Process:

(1) create a PHP script file. First, connect to the MySQL database server, connect to the database named db_while in MySQL, and set the page encoding format. Then, define the variable variables $ a and $ B, set the condition of the loop in the 1st-layer while loop to $ B <$ a, and then use the while statement to output the data in the data table again, and execute the data deletion operation. The Code is as follows:

The Code is as follows:

<? Php
If ($ _ POST [sub]) {// obtain parameters through POST
$ Conn = mysql_connect ("localhost", "root", "111"); // connect to the mysql database
Mysql_select_db ("db_while", $ conn); // connect to the database
Mysql_query ("set names gbk"); // defines the encoding format
$ A = $ _ POST [te]; // receives text box Parameters
$ B = 0; // define the variable
While ($ B <$ a) {// while LOOP
$ Rs = mysql_query ("select * from tb_while"); // execute the query operation
While ($ rst = mysql_fetch_array ($ rs) {// Save the query results in the array
$ SQL = "delete * from tb_while where id = $ B"; // SQL statement
Mysql_query ($ SQL); // execute the delete operation
Echo "the employee whose name is". $ rst [name]. "has been deleted <br>"; // output the name of the employee to be deleted.
$ B ++
}
}
}
?>


(2) store the file in the MR 2 66 folder and name it index. php.

 

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

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.