How to delete records in php

藏色散人
Release: 2023-03-01 21:58:02
Original
2181 people have browsed it

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.

How to delete records in php

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

Back to view

< ?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=&#39;dodel.php?id=$row->id&#39;>del</a></td><td>$row->depart</td><td>$row->ename</td></tr>";
echo "</table>";
?>
Copy after login

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);
?>
Copy after login

Description: used for MySQL data The deleted SQL statement is:

delete from 表名 where 条件=值
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!