php 使用PHP从MySQL中的表中删除特定行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20276645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:01:13  来源:igfitidea点击:

Deleting specific row from a table in MySQL using PHP

phpmysql

提问by Rendy Setiadi

I know this question has been asked too many times but I've searched and found nothing to solve my problem. In the table I have 4 columns: id(auto increment and primary key), type, quantityand date. The problem is when I press delete link it won't delete specific row I want and please forget about code injection, this is meant to be a sample program. Thanks.

我知道这个问题已经被问了太多次了,但我已经搜索过,但没有找到解决我的问题的方法。在表中,我有 4 列:(id自动增量和主键)typequantitydate。问题是当我按下删除链接时,它不会删除我想要的特定行,请忘记代码注入,这是一个示例程序。谢谢。

The code for a table is like this:

表的代码是这样的:

<div id="content" align="center">
<table border="1">
<tr>
<td>Item</td>
<td>Quantity</td>
<td>Date</td>
<td></td>
</tr>
<?php
include("connect.php");
$query=("SELECT * FROM purchase");
$result=mysql_query($query);
while($row=mysql_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['1'];?></td>
<td><?php echo $row['2'];?></td>
<td><?php echo $row['3'];?></td>
<td><a href="delete.php">Delete</a></td>
</tr>
<?php
}
?>
</table>
</div>

And the delete function:

和删除功能:

<?php
include("connect.php");

$host="localhost";
$user="root";
$pass="";
$db_name="proyek";
$tbl_name="purchase";

mysql_connect("$host", "$user", "$pass")or die("Cannot connect to SQL."); 
mysql_select_db('$db_name');
$query=("SELECT * FROM purchase");
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$id=$row[0];

mysql_query("DELETE from purchase WHERE id='$id'");

header("location:purchasehistoryadmin.php");
?>

回答by Jason Heo

when user clicks "Delete" then delete.php is called, so purchase id which want to be deleted should be transfer via delete.php,

当用户点击“删除”时,delete.php被调用,所以要删除的购买id应该通过delete.php传输,

as you about "Injection", below code is just example. moreover, CSRF is more dangerous, to preventing SQL Injection is easy, but CRSF is little bit difficult.

关于“注射”,下面的代码只是示例。而且CSRF比较危险,防止SQL注入很容易,但是CRSF有点困难。

list.php

列表.php

<td><a href="delete.php?id=<?echo $row['id'];?>">Delete</a></td>

delete.php

删除.php

$id = $_GET['id'];
mysql_query("DELETE from purchase WHERE id='$id'");

回答by Mário Kapusta

You visited delet.php but you didnt say which id you want to delete. You have to build url like

你访问了delet.php但是你没有说你要删除哪个id。你必须建立像

delete.php?param=5

delete.php?param=5

use $_GET to catch id and pass it to query.( Now you are deleting just first row)

使用 $_GET 来捕获 id 并将其传递给查询。(现在您只删除第一行)

Also check this http://www.w3schools.com/php/php_forms.asp

还要检查这个http://www.w3schools.com/php/php_forms.asp

Or

或者

http://php.net/manual/en/reserved.variables.get.php

http://php.net/manual/en/reserved.variables.get.php