In this tutorial, we will learn how to delete the record or data in PostgreSQL using PHP.
The File structure for this tutorial
dbcon.php:Â This is used for PostgreSQL database connection with PHP.
read.php:Â This is used for HTML Table and we will also put the PHP code here for data fetch/read.
Code for PostgreSQL connection with PHP (dbcon.php)
<?php // Database configuration $host = 'localhost'; $db = 'testdb'; // Here you can use your datbase name $user = 'postgres'; $pass = 'Test@123'; // Here you can your PostgreSQL DB user password $port = '5432'; // Default port for PostgreSQL // Create connection string $conn_string = "host=$host port=$port dbname=$db user=$user password=$pass"; // Establish a connection to the PostgreSQL database $conn = pg_connect($conn_string); if (!$conn) { echo "Error: Unable to open databasen"; exit; }
create read.php file. For fetching a record we have to get the row id of that record and store in $rid
. We access the $_GET[‘deleteid’]
 variable to do it.
Code for gets a record based on the given id.
<table class="table table-striped mt-4"> <thead> <tr> <th>#</th> <th>Name</th> <th>Email Id</th> <th>Mobile No</th> <th>Department</th> <th>Creation Date</th> <th>Action</th> </tr> </thead> <tbody> <?php $query= pg_query($conn,"select * from tblemployee"); $cnt=1; while($row=pg_fetch_array($query)){ ?> <tr> <td><?php echo $cnt;?></td> <td><?php echo $row['empname'];?></td> <td><?php echo $row['empemailid'];?></td> <td><?php echo $row['empmobileno'];?></td> <td><?php echo $row['empdepartment'];?></td> <td><?php echo $row['creationdate'];?></td> <td <a href="read.php?deleteid=<?php echo $row['id'];?>" class="btn btn-danger btn-sm">delete</a> </td> </tr> <?php $cnt++;} ?> </tbody> </table>
 Code for data deletion from the database. Put this code on top of the read.php file.
<?php include_once('dbcon.php'); //Code for deletion if(isset($_GET['deleteid'])) { $rid=intval($_GET['deleteid']); $sql=pg_query($conn,"delete from tblemployee where ID=$rid"); echo "<script>alert('Data deleted');</script>"; echo "<script>window.location.href = 'read.php'</script>"; } ?>
Here is the full code of read.php file
<?php include_once('dbcon.php'); //Code for deletion if(isset($_GET['deleteid'])) { $rid=intval($_GET['deleteid']); $sql=pg_query($conn,"delete from tblemployee where ID=$rid"); echo "<script>alert('Data deleted');</script>"; echo "<script>window.location.href = 'read.php'</script>"; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title>Fetch Data from PostgreSQL using PHP</title> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script> <style> body { color: #666; background: #f5f5f5; font-family: 'Roboto', sans-serif; } .table-responsive { margin: 30px 0; } table.table tr th, table.table tr td { border-color: #e9e9e9; } table.table-striped tbody tr:nth-of-type(odd) { background-color: #fcfcfc; } .table-wrapper { min-width: 1000px; background: #fff; padding: 20px; box-shadow: 0 1px 1px rgba(0,0,0,.05); } .pagination { margin: 10px 0 5px; } .pagination li a { border: none; min-width: 30px; min-height: 30px; color: #999; margin: 0 2px; line-height: 30px; border-radius: 4px !important; text-align: center; padding: 0; } .pagination li a:hover { color: #666; } .pagination li.active a, .pagination li.active a.page-link { background: #59bdb3; } .pagination li.active a:hover { background: #45aba0; } .pagination li:first-child a, .pagination li:last-child a { padding: 0 10px; } .pagination li.disabled a { color: #ccc; } .pagination li i { font-size: 17px; position: relative; top: 1px; margin: 0 2px; } </style> </head> <body> <div class="container-xl"> <div class="table-responsive"> <div class="table-wrapper"> <a href="index.php" class="btn btn-info">Add Record</a> <table class="table table-striped mt-4"> <thead> <tr> <th>#</th> <th>Name</th> <th>Email Id</th> <th>Mobile No</th> <th>Department</th> <th>Creation Date</th> <th>Action</th> </tr> </thead> <tbody> <?php $query= pg_query($conn,"select * from tblemployee"); $cnt=1; while($row=pg_fetch_array($query)){ ?> <tr> <td><?php echo $cnt;?></td> <td><?php echo $row['empname'];?></td> <td><?php echo $row['empemailid'];?></td> <td><?php echo $row['empmobileno'];?></td> <td><?php echo $row['empdepartment'];?></td> <td><?php echo $row['creationdate'];?></td> <td><a href="edit.php?id=<?php echo $row['id'];?>" class="btn btn-info btn-sm">Edit</a> <a href="read.php?deleteid=<?php echo $row['id'];?>" class="btn btn-danger btn-sm">delete</a> </td> </tr> <?php $cnt++;} ?> </tbody> </table> </div> </div> </div> </body> </html>
The post How to delete data from PostgreSQL using PHP appeared first on PHPGurukul.
Source: Read MoreÂ