Home >Backend Development >PHP Problem >How to delete records in php
How to delete records in php: first find the id to be deleted on the view page; then create a "delete.php" page; finally use the "delete from table name where condition = value" statement to delete.
PHP delete database record
Specific question:
I have a table in the database with two elements: id and name. Now I want to read an id value from a form, and then delete the page to delete a row of records corresponding to this id. The form's method=get, but Deleting the page cannot realize the function. Experts can help me look at the code: Thank you
100db36a723c770d327fc0aef2ce13b193f0f5c25f18dab9d176bd4f6de5d30eb2386ffb911b14667cb8f0f91ea547a7Delete member6e916e0f7d1e588d4f442bf645aedb2f9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
eae319b425d0c8257e9b639a39bd4f53Back to view5db79b134e9f6b82c0b36e0489ee08eddf250b2156c434f3390392d09b1c9563df250b2156c434f3390392d09b1c9563
49582f8046873217754b846390c61118
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e
Solution
1. First find the id to be deleted on the view page:
<?php $link=mysql_connect("localhost","root","管理员密码"); mysql_select_db("infosystem", $link); $q = "SELECT * FROM info"; mysql_query("SET NAMES GB2312"); $rs = mysql_query($q, $link); echo "<table>"; echo "<tr><td>部门名称</td><td>员工姓名</td><td>PC名称</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>"; ?>
2. Write a delete.php page with the following code:
<?php $link =mysql_connect("localhost","root","管理员密码"); mysql_select_db("infosystem", $link); $del_id=$_GET["id"]; $exec="delete from info where id=$del_id"; mysql_query($exec, $link); echo "删除成功!"; mysql_close($link); ?>
Description: used for MySQL data The deleted SQL statement is:
delete from 表名 where 条件=值
The value here is received through $del_id=$_GET["id"] and passed to the SQL statement. Finally, the SQL statement is deleted through mysql_query.
The above is the detailed content of How to delete records in php. For more information, please follow other related articles on the PHP Chinese website!