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




	CRUD: CReate, Update, Delete PHP MySQL - www.pakainfo.com


	

add index.php


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



index.php

// ...


	

To retrieve the database records and display



Member Name Location Action
Edit Delete
// ...

index.php


// newly added field


// modified form fields


Replace ..


With....


	

	

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.

Leave a Comment