PHP Delete Data from Database
Advertisements
Delete Data from Database in PHP
To delete any record from database on webpage first you need to create user interface so here i am going to create Html page and search box. First search record from database if it present then record will delete. To delete any record from database we use below MYSQL query.
Syntax
delete from table_name where id=2;
connect-db.php
Connect to database
<?php $server = 'localhost'; $user = 'user_name'; $pass = 'password'; $db = 'database_name'; // Connect to Database $connection = mysqli_connect($server, $user, $pass) or die("Could not connect to server ... \n" . mysqli_error ()); // select database mysqli_select_db($db) or die("Could not connect to database ... \n" . mysqli_error ()); ?>
delete.html
Delete page
<?php include 'db-connect.php'; mysqli_select_db("tutorohx_rw", $conncection); $valueToSearch = $_POST['valueToSearch']; $sql = "SELECT * FROM letter_dispatch WHERE s_no=$valueToSearch"; $result = mysqli_query($sql, $connection); ?> <?php if ($result) { while($row = mysqli_fetch_array($result)) { ?> // Show Record <h3>You Want to Delere Record:</h3> <table class="table" border='0'> <form method="post" action='delete-record-successfully.php' id="deleteform"> <tr><th>Serial no</th><td><span style="color:red; font-weight: bold; font-size:20px;"><?php echo $valueToSearch;?></span></td></tr> <tr><th></th><td><input type=hidden name=s_no value=<?php echo $row['s_no']; ?>></td></tr> <tr><td><input type="submit" form="deleteform" value="Delete"></td></tr> </form> <?php } } ?> </table>
delete-record-successfully.php
Record Deleted
<?php include 'db-connect.php'; mysqli_select_db("tutorohx_rw", $conncection); $s_no = $_POST['s_no']; $sql = "DELETE FROM letter_dispatch where s_no='$s_no'"; $result = mysqli_query($sql, $connection); if($result) { echo"Record Deleted Successfully........"; } else { echo"Try Again........"; } ?>
Google Advertisment