how to delete data from database in php using button?

Today, We want to share with you how to delete data from database in php using button?.In this post we will show you how to edit data from database in php using button?, hear for how to delete a row in mysql using php we will give you demo and example for implement.In this post, we will learn about PHP CRUD with an example.

how to edit/delete data from database in php using button?

simple PHP CRUD Create, edit, update and delete posts with MySQL database

index.php

<!DOCTYPE html>
<html>
<head>
	<title>CRUD: CReate, Update, Delete PHP MySQL - www.pakainfo.com</title>
</head>
<body>
	<form method="post" action="do_backend_api.php" >
		<div class="input-group">
			<label>Member Name</label>
			<input type="text" name="name" value="">
		</div>
		<div class="input-group">
			<label>Location</label>
			<input type="text" name="location" value="">
		</div>
		<div class="input-group">
			<button class="btn" type="submit" name="save" >Save</button>
		</div>
	</form>
</body>
</html>

add index.php

<link rel="stylesheet" type="text/css" href="style.css">

styles.css

body {
    font-size: 19px;
}
table{
    width: 50%;
    margin: 30px auto;
    border-collapse: collapse;
    text-align: left;
}
tr {
    border-bottom: 1px solid #cbcbcb;
}
th, td{
    border: none;
    height: 30px;
    padding: 2px;
}
tr:hover {
    background: #F5F5F5;
}

form {
    width: 45%;
    margin: 50px auto;
    text-align: left;
    padding: 20px; 
    border: 1px solid #bbbbbb; 
    border-radius: 5px;
}

.input-group {
    margin: 10px 0px 10px 0px;
}
.input-group label {
    display: block;
    text-align: left;
    margin: 3px;
}
.input-group input {
    height: 30px;
    width: 93%;
    padding: 5px 10px;
    font-size: 16px;
    border-radius: 5px;
    border: 1px solid gray;
}
.btn {
    padding: 10px;
    font-size: 15px;
    color: white;
    background: #5F9EA0;
    border: none;
    border-radius: 5px;
}
.edit_btn {
    text-decoration: none;
    padding: 2px 5px;
    background: #2E8B57;
    color: white;
    border-radius: 3px;
}

.del_btn {
    text-decoration: none;
    padding: 2px 5px;
    color: white;
    border-radius: 3px;
    background: #800000;
}
.msg {
    margin: 30px auto; 
    padding: 10px; 
    border-radius: 5px; 
    color: #3c763d; 
    background: #dff0d8; 
    border: 1px solid #3c763d;
    width: 50%;
    text-align: center;
}

php_code.php

<?php 
	session_start();
	$db = mysqli_connect('localhost', 'root', '', 'members_application');

	// initialize variables
	$name = "";
	$location = "";
	$id = 0;
	$update = false;

	if (isset($_POST['save'])) {
		$name = $_POST['name'];
		$location = $_POST['location'];

		mysqli_query($db, "INSERT INTO members (name, location) VALUES ('$name', '$location')"); 
		$_SESSION['message'] = "Location saved"; 
		header('location: index.php');
	}

// ...

<?php  include('do_backend_api.php'); ?>

index.php

// ...
<body>
<?php if (isset($_SESSION['message'])): ?>
	<div class="msg">
		<?php 
			echo $_SESSION['message']; 
			unset($_SESSION['message']);
		?>
	</div>
<?php endif ?>

To retrieve the database records and display

<?php $results = mysqli_query($db, "SELECT * FROM members"); ?>

<table>
	<thead>
		<tr>
			<th>Member Name</th>
			<th>Location</th>
			<th colspan="2">Action</th>
		</tr>
	</thead>
	
	<?php while ($row = mysqli_fetch_array($results)) { ?>
		<tr>
			<td><?php echo $row['name']; ?></td>
			<td><?php echo $row['location']; ?></td>
			<td>
				<a href="index.php?edit=<?php echo $row['id']; ?>" class="edit_btn" >Edit</a>
			</td>
			<td>
				<a href="do_backend_api.php?del=<?php echo $row['id']; ?>" class="del_btn">Delete</a>
			</td>
		</tr>
	<?php } ?>
</table>

<form>
    // ...

index.php

<?php 
	if (isset($_GET['edit'])) {
		$id = $_GET['edit'];
		$update = true;
		$record = mysqli_query($db, "SELECT * FROM members WHERE id=$id");

		if (count($record) == 1 ) {
			$n = mysqli_fetch_array($record);
			$name = $n['name'];
			$location = $n['location'];
		}
	}
?>

// newly added field
<input type="hidden" name="id" value="<?php echo $id; ?>">

// modified form fields
<input type="text" name="name" value="<?php echo $name; ?>">
<input type="text" name="location" value="<?php echo $location; ?>">

Replace ..

<button class="btn" type="submit" name="save" >Save</button>

With….

Read Also:  sql multiple subqueries in (select statement)

<?php if ($update == true): ?>
	<button class="btn" type="submit" name="update" style="background: #556B2F;" >update</button>
<?php else: ?>
	<button class="btn" type="submit" name="save" >Save</button>
<?php endif ?>

php_code.php

// ... 

if (isset($_POST['update'])) {
	$id = $_POST['id'];
	$name = $_POST['name'];
	$location = $_POST['location'];

	mysqli_query($db, "UPDATE members SET name='$name', location='$location' WHERE id=$id");
	$_SESSION['message'] = "Location updated!"; 
	header('location: index.php');
}

php_code.php

if (isset($_GET['del'])) {
	$id = $_GET['del'];
	mysqli_query($db, "DELETE FROM members WHERE id=$id");
	$_SESSION['message'] = "Location deleted!"; 
	header('location: index.php');
}

I hope you get an idea about how to add a delete button in front of each user record in php?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or opinions about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.