delete a specific row of a table using php

Sam

The question is very basic and i found many similar questions in stackoverflow 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

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

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, $id 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.

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.


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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

delete a specific row of a table using php

From Dev

Delete specific row in CSV file using HTML Table

From Dev

How to delete a specific row of a dynamic table using JQuery

From Dev

Deleting specific row from a table in MySQL using PHP

From Dev

Delete Table row using javascript

From Dev

Delete row of the table using javascript

From Dev

Updating specific row on a table PHP

From Dev

delete a row from table based on a specific association

From Dev

Delete multiple table cells from specific row

From Dev

delete a row from table based on a specific association

From Dev

How to delete row using php?

From Dev

Edit specific row of a table using id of that row

From Dev

How to delete a specific row of a dynamic table after prompting the user using JQuery?

From Dev

how to delete the specified row from html table and also in mysql table using php

From Dev

PHP button delete row from table not working

From Dev

AJAX Delete row in table with PHP id selected

From Dev

Delete row from database with a button in table (PHP)

From Dev

Delete specific row in database table and generatet html table

From Dev

unable to delete row from table using jquery?

From Dev

delete the last row in a table using sql query?

From Dev

How to delete a row in a table using jQuery?

From Dev

Delete an entire row from a table, using MySQL

From Dev

unable to delete row from table using jquery?

From Dev

delete any table row using jQuery

From Dev

Delete table row in AutoCAD using C#

From Dev

To delete a row dynamically from a table using javascript

From Dev

PHP form delete from MYSQL table dont delete the right row

From Dev

get value of a specific row of a table using jquery

From Dev

How to properly delete a row using PHP & PDO

Related Related

  1. 1

    delete a specific row of a table using php

  2. 2

    Delete specific row in CSV file using HTML Table

  3. 3

    How to delete a specific row of a dynamic table using JQuery

  4. 4

    Deleting specific row from a table in MySQL using PHP

  5. 5

    Delete Table row using javascript

  6. 6

    Delete row of the table using javascript

  7. 7

    Updating specific row on a table PHP

  8. 8

    delete a row from table based on a specific association

  9. 9

    Delete multiple table cells from specific row

  10. 10

    delete a row from table based on a specific association

  11. 11

    How to delete row using php?

  12. 12

    Edit specific row of a table using id of that row

  13. 13

    How to delete a specific row of a dynamic table after prompting the user using JQuery?

  14. 14

    how to delete the specified row from html table and also in mysql table using php

  15. 15

    PHP button delete row from table not working

  16. 16

    AJAX Delete row in table with PHP id selected

  17. 17

    Delete row from database with a button in table (PHP)

  18. 18

    Delete specific row in database table and generatet html table

  19. 19

    unable to delete row from table using jquery?

  20. 20

    delete the last row in a table using sql query?

  21. 21

    How to delete a row in a table using jQuery?

  22. 22

    Delete an entire row from a table, using MySQL

  23. 23

    unable to delete row from table using jquery?

  24. 24

    delete any table row using jQuery

  25. 25

    Delete table row in AutoCAD using C#

  26. 26

    To delete a row dynamically from a table using javascript

  27. 27

    PHP form delete from MYSQL table dont delete the right row

  28. 28

    get value of a specific row of a table using jquery

  29. 29

    How to properly delete a row using PHP & PDO

HotTag

Archive