Remove specific table row using php

The question is very simple and I found many similar questions on stackoverflow, but none of them worked as expected for me.

I 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,

<?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

<?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");
?> 

      

Database browsing

Id  name
1   abc
2   cdf

      

the problem is it doesn't delete data and also doesn't show any errors

I am new to this field, would appreciate it if someone could help me

+3


source to share


3 answers


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 pointed out in the comment, $id

it was not defined).

<?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.

However, for security purposes you should learn with trained statements or PDO with trained statements , they are much safer. I have provided an example below. mysqli


Here's an example of prepared statements:

<?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();

      

+5


source


Add a parameter to your link GET

:

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

      



Then intercept it in the request delete.php

:

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

      

0


source


you probably don't 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 full code from below link: remove single row from database using ajax

-1


source







All Articles