How to delete data from database

How to delete data from Mysql database using Php

Source code


									// DB credentials.
									// Establish database connection.
										$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
									catch (PDOException $e)
										exit("Error: " . $e->getMessage());


									include "config.php";
										$sql="delete from products where id=:cid";
										$query2= $dbh->prepare($sql);
										if($query2->execute()) {
											echo "<script>alert('product Deleted');</script>" ;
											echo "<script>window.location.href ='index.php'</script>";
											echo "<script>alert('Failed try again later.');</script>" ;


									<!DOCTYPE HTML>
									<html lang="en">
										<meta charset="utf-8">
										<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
										<title>How to delete Mysql data in php</title>
										<!--Bootstrap -->
										<link rel="stylesheet" href="css/bootstrap.css" type="text/css">
										<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css">
										<div class="container">
											<h3>How to delete Mysql data in php</h3>
											<div class="  d-flex flex-row align-items-center justify-content-between">
												<h6 class="m-0 font-weight-bold text-primary">Add product</h6>
												<div class="card-tools" style="float: right;">
													<button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#deposit" ><i class="fas fa-plus" ></i> Add Product
											<div> </div>
											<div class="row ">
												<table id="" class="table table-bordered">
															<th class="text-center">No</th>
															<th>Product Name</th>
															<th class="text-center"> Product Category</th>
															<th class="text-center">Product Price</th>
															<th class="text-center">Posting Date</th>
															<th class=" text-center">Action</th>
														$sql="SELECT * from products ORDER BY id DESC";
														$query = $dbh -> prepare($sql);
														if($query->rowCount() > 0)
															foreach($results as $row)
																	<td class="text-center"><?php echo htmlentities($cnt);?></td>
																	<td><?php  echo htmlentities($row->ProductName);?></td>
																	<td class="text-center"><?php  echo htmlentities($row->CategoryName);?></td>
																	<td class="text-center"><?php  echo htmlentities($row->ProductPrice);?></td>
																	<td class="text-center"><?php  echo htmlentities(date("d-m-Y", strtotime($row->PostingDate)));?></td>
																	<td class=" text-center">
																		<a href="index.php?del=<?php echo $row->id;?>" class=" btn btn-sm btn-danger"  data-toggle="tooltip" data-original-title="Delete" onclick="return confirm('Do you really want to delete?');"> Delete </a>
														} ?>
										<!-- Loading Scripts -->
										<script src="js/jquery.min.js"></script>
										<script src="js/bootstrap.min.js"></script>


John Simith

Thank you code4berry may the lord reward you

Kellen Brillian

Very nice code I like everything, but help and take my donation

Leave a Comment:

You Might Also Like

hostel booking

Hostel booking management system
Learn More

car rental

Car rental management system in Php
Learn More

student details

Student details management system
Learn More


Tourism management system in Php and Mysql
Learn More

Latest Tutorial

sweet alerts

How to delete table row using sweet alert2
Learn More


How to create piechart with Mysql data
Learn More

register and login

How to register and login in php
Learn More

edit data

How to edit Mysql data in modal using php
Learn More