使用php删除表的特定行

时间:2023-01-20 18:56:07

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

这个问题非常基础,我在*发现了很多类似的问题,但是没有一个对我来说是正确的。

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

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

3 个解决方案

#1


5  

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, $id was not defined).

然后删除。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 mysqli with 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();

#2


0  

Add a GET parameter to your link:

在链接中添加一个GET参数:

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

Then catch it in your delete.php query:

然后在删除中捕获它。php查询:

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

#3


-1  

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从databse中删除单个行

#1


5  

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, $id was not defined).

然后删除。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 mysqli with 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();

#2


0  

Add a GET parameter to your link:

在链接中添加一个GET参数:

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

Then catch it in your delete.php query:

然后在删除中捕获它。php查询:

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

#3


-1  

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从databse中删除单个行