Delete Data Using PHP MySQLi
PHP MySQLi Delete Records |
Hi Guys in This Tutorial we will Learn how to Delete Data from MySQL Database using PHP MySQLi.
Database Structure:
Database name: tutorialTable Name: test
CREATE TABLE IF NOT EXISTS `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(150) NOT NULL, `email` varchar(150) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
INSERT INTO `test` (`id`, `name`, `email`) VALUES (12, 'ANJANBD', 'Anjankumardhali6@gmail.com'), (11, 'Krishna', 'krishna@mail.com'), (3, 'Rupam Mondal', 'RupomMondal@gmail.com'), (4, 'ANJAN', 'KumarANjan@gmail.com'), (5, 'Biswas Shiuli', 'ShiuliBiswas@gmail.com'), (7, 'Hrithik', 'Hrithik@gmail.com'), (8, 'Modhu Bonik', 'Modhu360@gmail.com'), (9, 'Rupam Mondal Joy', 'RupomMondal@gmail.com'), (13, 'Rupa', 'Rupa@hotmail.com'), (14, 'ANJAN', 'Kumar@gmail.com'), (15, 'ANJANBD', 'Anjankumardhali6@gmail.com'), (16, 'Krishna', 'krishna@mail.com'), (17, 'Rupam Mondal', 'RupomMondal@gmail.com'), (18, 'ANJAN', 'KumarANjan@gmail.com'), (19, 'Biswas Shiuli', 'ShiuliBiswas@gmail.com'), (20, 'Hrithik', 'Hrithik@gmail.com'), (21, 'Modhu Bonik', 'Modhu360@gmail.com'), (22, 'Rupam Mondal Joy', 'RupomMondal@gmail.com'), (23, 'Rupa', 'Rupa@hotmail.com'), (24, 'ANJAN', 'Kumar@gmail.com'), (25, 'ANJANBD', 'Anjankumardhali6@gmail.com'), (26, 'ANJANBD60', 'Anjankumardhali6@gmail.comss'), (27, 'Priyoshi', 'Priyoshi@gmail.com'), (28, 'Rupom Chakma', 'Rupam@gmail.com'), (29, 'Monisha Roy', 'Monisharoy@gmail.com');
Directory or Folder and files Structure:
Create 4 files in any directory under htdocs folder.1.db_config.php
2.index.php [Displaying Data with Delete Button]
3.delete.php
4.style.css
PHP MySQLi Delete Records Folder Structure |
Codes for db_config.php
<?php $host = "localhost"; //Host name $username = "root"; //MySQL Username .'root' is default $password = ""; //MySQL Password $db_name="tutorial"; //Database Name // Create connection passing host,username,password and db_name $conn = new mysqli($host, $username, $password,$db_name); // Checking connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ?>
Codes for index.php
<link rel="stylesheet" href="style.css" type="text/css"/> <?php include("db_config.php");; $sql = "SELECT id,name, email FROM test ORDER by id ASC "; $result = $conn->query($sql); if ($result->num_rows > 0) { echo "<table>"; echo "<tr>"; echo "<th>ID</th>"; echo "<th>Name</th>"; echo "<th>Email</th>"; echo "<th>Action</th>"; echo "</tr>"; // output data of each row while($row = $result->fetch_assoc()) { echo "<tr>"; echo "<td>$row[id]</td>"; echo "<td>$row[name]</td>"; echo "<td>$row[email]</td>"; echo "<td><a href=delete.php?id=$row[id]>Delete</a></td>"; echo "</tr>"; } echo "</table>"; } else { echo "0 results"; } $conn->close(); ?>
Codes for delete.php
<?php include('db_config.php'); //Getting ID Value from Index page $id=$_GET['id']; // sql to delete record form test table passing ID $sql = "DELETE FROM test WHERE id=$id"; if ($conn->query($sql) === TRUE) { header("location:index.php"); } else { echo "Error deleting record: " . $conn->error; } $conn->close(); ?>
Codes for style.css
table th, td{ border: 1px solid green; text-align:left; } th,td{ padding:8px; } table{ border-collapse:collapse; width:50%; } table td a{ background-color: #4CAF50; border: none; color: white; padding: 7px 12px; text-decoration: none; margin: 4px 2px; cursor: pointer; } th{ padding:15px; background:lightgreen; font-size:20px; font-weight:bold; } tr:nth-child(odd){background:#808080} }
If you Like This article Please Like and Share With Your Friends.
Keep Visiting for More Codes.Thank you.
Awesome article.
ReplyDeleteHi there, all is going nicely here and ofcourse every one
ReplyDeleteis sharing facts, that's in fact excellent, keep up writing.
It's an awesome article in favor of all the web visitors; they will obtain benefit from it I am sure.
ReplyDeleteIf some one wishes to be updated with most up-to-date technologies then he must be pay a
ReplyDeletevisit this website and be up to date all the time.
That's Great..Keep it up..
ReplyDelete