Delete Data Using PHP/MYSQL With Javascript Confirmation

0
577

Hello. Good Day! Everyone! Today I’m gonna teach you on creating a simple idea on Delete Data Using PHP/MYSQL With Javascript Confirmation.

As what you read. This not the uncommon tutorial for deleting data. I add twist in my tutorial. So now, you can add a confirmation dialog which would answerable by either “yes” or “no“.

So now let’s start our tutorial.

So first create a database, name it as any name you desire. In my case, I user “itsourcecode” as the name of my database.

Create a table “names” then put the following attributes.

[sql]CREATE TABLE names ( name_id int(11) NOT NULL, full_name text NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1;[/sql]

Now for the php connection to the database. Create a “connection.php” file then put the following codes.

[php]<?php
$mysqli = new mysqli(‘localhost’, ‘root’, ”, ‘itsourcecode’);
?>[/php]

On the index page. Just put the following codes.

[php]<?php include ‘connection.php’; ?>
<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8″>
<title>Insert and Delete Data in PHP/MYSQL</title>

function confirmDelete(id) {
if (confirm(“Are you sure you want to delete this data?”)) {
window.location.href = “delete.php?id=” + id;
} else {
return false;
}
}

</head>
<body>
<h3>Insert and Delete Data in PHP/MYSQL</h3>
<form method=”post” action=”insert.php”>
<label>Enter fullname here:</label><br>
<input type=”text” name=”fullname” /><br>
<input type=”submit” name=”enter_fullname” value=”Add” />
</form>
<br>
<?php
if (isset($_GET[‘delete_action’])) {
if ($_GET[‘delete_action’] == “success”) {
echo ‘Successfully Deleted!<br><br>’;
}
}
?>
<?php
$names = $mysqli->query(“SELECT * FROM names”);
while ($data = $names->fetch_assoc()) { ?>
<b>#<?php echo $data[‘name_id’] ?></b> <?php echo $data[‘full_name’] ?> | <button onclick=”confirmDelete(<?php echo $data[‘name_id’] ?>);”>Delete</button><br>
<?php }
?>
</body>
</html>
[/php]

Now, for inserting the data first before deleting. Of course, there should be a data to be inserted first.

Create a “insert.php” file then put the following codes.

[php]<?php
include ‘connection.php’;
if (isset($_POST[‘enter_fullname’])) {
$fullname = $_POST[‘fullname’];
$insert = $mysqli->query(“INSERT INTO names (full_name) VALUES (‘$fullname’)”);
if ($insert) {
header(“Location: index.php”);
} else {
echo $mysqli->error;
}
}
?>[/php]

Now for deleting the data. Create a “delete.php” file then put the following codes.

[php]<?php
include ‘connection.php’;
if (isset($_GET[‘id’])) {
$id = $_GET[‘id’];
$delete = $mysqli->query(“DELETE FROM names WHERE name_id = $id”);
if ($delete) {
header(“Location: index.php?delete_action=success”);
} else {
echo $mysqli->error;
}
}
?>[/php]

Screenshots:

If you have questions regarding the tutorial about Delete Data Using PHP/MYSQL With Javascript Confirmation Feel free to ask us by commenting below or by visiting or our contact page. Thank you.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.