Delete Data Using PHP/MYSQL With Javascript Confirmation

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.

Frequently Asked Questions

How does this PHP project work?

Built with vanilla PHP (no framework) and MySQL backend. Standard structure: form HTML, PHP script handlers, MySQL via PDO or mysqli, sessions for auth, Bootstrap for responsive layout. Ready to extend for BSIT capstone scope.

What PHP and MySQL versions does this project require?

Most projects in this batch run on PHP 7.4 to PHP 8.2 with MySQL 5.7+ or MariaDB 10+. A few older projects need PHP 5.6 (deprecated, use XAMPP 7.x). To run: install XAMPP / WAMP / Laragon, extract project to htdocs, import the included .sql file via phpMyAdmin, edit the connection file (usually config.php or db_connect.php) with your DB credentials, browse to the project URL in your browser.

How do I set up the database for this PHP project?

Open phpMyAdmin (http://localhost/phpmyadmin/ on XAMPP), create a new empty database with the name specified in the project’s config.php. Click the Import tab, choose the included .sql file, click Go. Then edit config.php (or includes/connection.php) with: ‘localhost’, your MySQL username (usually ‘root’), your MySQL password (usually blank for XAMPP), and the database name.

Can I use this PHP project for a BSIT capstone or thesis?

Yes, but extend it. A bare CRUD app is too narrow for full capstone scope. Add: user roles via session checks, reports/dashboards (Chart.js + AJAX), PDF exports (TCPDF library), email notifications (PHPMailer), real domain extension (analytics, audit log, multi-branch support). Pair with Chapter 1-5 documentation matching your panel’s rubric.

Why am I getting ‘connection error’ or ‘undefined function mysqli_connect’?

Three common PHP issues: (1) Connection error: Apache + MySQL services not running in XAMPP control panel, OR database name in config.php does not match what you created in phpMyAdmin. (2) ‘undefined function mysqli_connect’: MySQL extension not enabled, in php.ini uncomment extension=mysqli (then restart Apache). (3) ‘No such file or directory’: MySQL socket path wrong, use 127.0.0.1 instead of localhost in the connection string.

Where can I find more PHP projects with source code?

Browse the PHP Projects hub for the full library (300+ vanilla PHP systems). For modern PHP MVC alternatives see Laravel Projects (74 systems) or CodeIgniter Projects (32 systems). For BSIT-focused capstone idea lists see 150 Best Capstone Project Ideas.

Related PHP Projects

Leave a Comment