Use PHP to delete MySQL records

Source: Internet
Author: User

First, create the data display page view. php:
Copy codeThe Code is as follows:
<? Php
$ Link = mysql_connect ("localhost", "root", "administrator password ");
Mysql_select_db ("infosystem", $ link );
$ Q = "SELECT * FROM info ";
Mysql_query ("set names GB2312 ");
$ Rs = mysql_query ($ q, $ link );

Echo "<table> ";
Echo "<tr> <td> Department name </td> <td> employee name </td> <td> PC name </td> </tr> ";
While ($ row = mysql_fetch_object ($ rs) echo "<tr> <td> <a href = 'dodel. php? Id = $ row-> id'> del </a> </td> <td> $ row-> depart </td> <td> $ row-> ename </ td> </tr> ";
Echo "</table> ";
?>

Unlike the general data display page, this time, there is a connection with "del" before each record. By clicking del, we can delete the corresponding data records in MySQL. The red part of the above program indicates that when del is clicked, the ID number of the corresponding record is passed to dodel. php.

Next let's see dodel. php:
Copy codeThe Code is as follows:
<? Php
$ Link = mysql_connect ("localhost", "root", "administrator password ");
Mysql_select_db ("infosystem", $ link );
$ Del_id = $ _ GET ["id"];
$ Exec = "delete from info where id = $ del_id ";
Mysql_query ($ exec, $ link );
Echo "deleted successfully! ";
Mysql_close ($ link );
?>

This page is the key to this topic. It receives the ID number from view. php and substitutes it into the SQL statement used to delete the record, so as to delete the record.

Let's take a closer look. The SQL statement used to delete MySQL Data is:

Delete from table name where condition = Value
The value here is received through $ del_id = $ _ GET ["id"], passed to the SQL statement, and executed through mysql_query.

Okay. Can you check whether the data in MySQL has been deleted correctly? If you have any questions, please leave a message and ask questions ~

By Sunec
Original load: Cenus Blog

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.