php 使用php删除表的特定行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26107666/
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 18:09:43  来源:igfitidea点击:

delete a specific row of a table using php

phpmysqlsqlmysqli

提问by Sam

The question is very basic and i found many similar questions in stackoverflow but none of them worked properly for me.

这个问题非常基本,我在 stackoverflow 中发现了许多类似的问题,但没有一个对我来说正常工作。

I have designed a table that will display data something like this:

我设计了一个表格来显示如下数据:

ID  name    Delete
1   abc     Delete
2   def     Delete

the code used for the above display is

用于上述显示的代码是

<?php
$con=mysqli_connect("abc","abc","abc","abc");
// Check connection
if (mysqli_connect_errno()) 
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM student");
echo "<table class='table table-striped table-bordered table-hover'>
<thead>
<tr>
<th>ID</th>
<th>name</th>
<th>delete</th>   
</tr>
</thead>";
while($row = mysqli_fetch_array($result)) 
{
echo "<tbody data-link='row' class='rowlink'>";
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td><a href='delete.php'>Delete</a></td>";
echo "</tr>";
echo "</tbody>";    
}
echo "</table>";
mysqli_close($con);
?>

code for delete.php

delete.php 的代码

<?php
$con=mysqli_connect("abc","abc","abc","abc");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"DELETE FROM student WHERE id='$id'");
mysqli_close($con);
header("Location: index.php");
?> 

View of database is

数据库视图是

Id  name
1   abc
2   cdf

the problem is that it is not deleting the data and is also not showing any error

问题是它没有删除数据,也没有显示任何错误

I am new to this field, would be grateful if someone could help me

我是这个领域的新手,如果有人能帮助我,我将不胜感激

回答by Funk Forty Niner

Change this line:

更改此行:

echo "<td><a href='delete.php'>Delete</a></td>";

to

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

Then for delete.php(and as originally stated in a comment, $idwas not defined).

然后对于delete.php(正如最初在评论中所述,$id未定义)。

<?php
$con=mysqli_connect("abc","abc","abc","abc");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$id = $_GET['id']; // $id is now defined

// or assuming your column is indeed an int
// $id = (int)$_GET['id'];

mysqli_query($con,"DELETE FROM student WHERE id='".$id."'");
mysqli_close($con);
header("Location: index.php");
?> 

and it will work.

它会起作用。

Yet, for safety purposes, you should look into using mysqliwith prepared statements, or PDO with prepared statements, they much safer. I have included an example below.

然而,为了安全起见,你应该考虑使用mysqli预处理语句,或PDO预处理语句它们更安全。我在下面包含了一个例子。



Here is a prepared statements example:

这是一个准备好的语句示例:

<?php 
$DB_HOST = "xxx";
$DB_NAME = "xxx";
$DB_USER = "xxx";
$DB_PASS = "xxx";

$con = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($con->connect_errno > 0) {
  die('Connection failed [' . $con->connect_error . ']');
}

$id = (int)$_GET['id'];

$update = $con->prepare("DELETE FROM student WHERE id = ?");
$update->bind_param('i', $id);
$update->execute();
$update->close();

回答by S.Pols

Add a GETparameter to your link:

GET向链接添加参数:

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

Then catch it in your delete.phpquery:

然后在您的delete.php查询中捕获它:

$id = $_GET['id'];
mysqli_query($con,"DELETE FROM student WHERE id='".$id."'");

回答by Danyal Fayyaz

you probably dont want to refresh the page every time a row is deleted you can do this using an ajax request to the server much faster. You can get the full code from the link below : delete individual row from databse using ajax

您可能不想在每次删除一行时刷新页面,您可以使用对服务器的 ajax 请求更快地执行此操作。您可以从以下链接获取完整代码:使用 ajax 从数据库中删除单个行