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
Delete member < ?php
$link=mysql_connect("localhost","root","");
$db=mysql_select_db("aaa");
mysql_query(" set names utf8",$link);
$sql="delete from a where id='.$_GET['id']';//The error should be here
$result =mysql_query($sql,$link);
echo "Delete successfully! ";
?>
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!