How to add a delete button to a PHP form that will delete a row from a MySQL table
You have to pass a variable in the delete link. You have to pass <?php echo $contact['name']; ?>
(the name
value) in a hidden field or pass this value in URL
:
Replace
<td class="contact-delete">
<form action='delete.php' method="post">
<input type="hidden" name="name" value="">
<input type="submit" name="submit" value="Delete">
</form>
</td>
With
<td class="contact-delete">
<form action='delete.php?name="<?php echo $contact['name']; ?>"' method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
</td>
Adding a delete button in PHP on each row of a MySQL table
Simply using PHP as follows (You can use JS)
while($book = mysqli_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$book['Staff_ID']."</td>";
echo "<td>".$book['Staff_Name']."</td>";
echo "<td>".$book['Class']."</td>";
echo "<td><a href='delete.php?id=".$book['Staff_ID']."'></a></td>"; //if you want to delete based on staff_id
echo "</tr>";
}// end while loop
In your delete.php
file,
$id = $_GET['id'];
//Connect DB
//Create query based on the ID passed from you table
//query : delete where Staff_id = $id
// on success delete : redirect the page to original page using header() method
$dbname = "your_dbname";
$conn = mysqli_connect("localhost", "usernname", "password", $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// sql to delete a record
$sql = "DELETE FROM Bookings WHERE Staff_ID = $id";
if (mysqli_query($conn, $sql)) {
mysqli_close($conn);
header('Location: book.php'); //If book.php is your main page where you list your all records
exit;
} else {
echo "Error deleting record";
}
How to add a button to my PHP form that deletes rows from my MYSQL database
In your html view page some change echo "<td><a href='delete.php?did=".$row['id']."'>Delete</a></td>";
like bellow:
<?php
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['rollnumber'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td><a href='delete.php?did=".$row['id']."'>Delete</a></td>";
echo "</tr>";
}
?>
PHP delete code :
<?php
if(isset($_GET['did'])) {
$delete_id = mysql_real_escape_string($_GET['did']);
$sql = mysql_query("DELETE FROM venu WHERE id = '".$delete_id."'");
if($sql) {
echo "<br/><br/><span>deleted successfully...!!</span>";
} else {
echo "ERROR";
}
}
?>
Note : Please avoid mysql_*
because mysql_*
has beed removed from
PHP 7. Please use mysqli
or PDO
.
More details about of PDO
connection http://php.net/manual/en/pdo.connections.php
And more details about of mysqli
http://php.net/manual/en/mysqli.query.php
PHP delete button for every row / change value in table for this row
Query
This query:
$sql = "SELECT name, email, code, status, lname, checkin, checkout, mnumber, totalprice, rescode FROM usertable";
$result = $conn->query($sql);
should be
$sql = "SELECT id, name, email, code, status, lname, checkin, checkout, mnumber, totalprice, rescode FROM usertable";
$result = $conn->query($sql);
Delete link
This html code
<td><a href="delete.php">Cancel</a></td>
should be
<td><a href="delete.php?id=' . $row['id'] . '">Cancel</a></td>
Please, ... check your code: you concat strings using "," instead of ".".
Finally
This SQL code
$sql = "DELETE FROM usertable WHERE id='10'";
should be
$sql = "DELETE FROM usertable WHERE id='" . $_GET['id'] . "'";
adding a delete button in php
Before your table:
<form action="" method="post">
and close the form tag after your table.
Place this code as your delete button:
echo '<td><button type="submit" name="deleteItem" value="'.$row['id'].'" />Delete</button></td>"';
In PHP you should do the following
<?php
if(isset($_POST['deleteItem']) and is_numeric($_POST['deleteItem']))
{
// here comes your delete query: use $_POST['deleteItem'] as your id
// $delete = $_POST['deleteItem']
// $sql = "DELETE FROM `tablename` where `id` = '$delete'";
}
?>
Add a delete button to a table populated from a database
To remove a row from database you need to use a DELETE
statement with a primary key
, which you need to pass from this while loop.
Make a link inside while loop: [Demo]
<a href='delete.php?id=your_id'>Delete</a>
Now in your delete page, you need to capture or store the id using $_GET
and using the DELETE Statement you can simply delete row from database.
DELETE FROM table_name WHERE primary_key=your_get_value;
Note: In your delete page just make a query for delete the row also
make some security.
Delete row from database with a button in table (PHP)
you can do this by two ways if you dont want use Ajax
1.GET method
2.POST method
you can do this with GET method by passing id in url example-
echo '<a href="http://www.yourwebsite.com/yourfile.php?uid='.$results['course_id'].'"></a>';
then you can get this value. example-
if(isset($_GET['uid'])){
$courseid = $_GET['uid'];
}
or you can do with POST method .then you need to create a form and send value in hidden field.
example -
<form method="post" action="">
<input type="hidden" name="delete_course_id" value="<?php echo $results['course_id']; ?>">
<button type="submit" name="delete_course" class="btn btn-danger">Borrar</button>
</form>
and you will get this id after form post like
if(isset($_POST['delete_course']))
{
$courseid = strip_tags($_POST['delete_course_id']);
}
Related Topics
How to Fetch All in Assoc Array from a Prepared Statement
Setting Value of a HTML Form Textarea
PHP Add Elements to Multidimensional Array with Array_Push
Determining What Classes Are Defined in a PHP Class File
PHP Header() Redirect with Post Variables
Title Case a String Containing One or More Last Names While Handling Names with Apostrophes
Using PHP Replace Spaces in Urls with %20
Get a PHP Object Property That Is a Number
How to Sort an Array of Utf-8 Strings in PHP
How to Integrate Nodejs + Socket.Io and PHP
Email from PHP Has Broken Subject Header Encoding
What Does Filter_Sanitize_String Do
Using PHP Substr() and Strip_Tags() While Retaining Formatting and Without Breaking HTML
How to Set .Env Values in Laravel Programmatically on the Fly