How to delete data from PostgreSQL using PHP
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?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 database\n"; 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<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.
1 2 3 4 5 6 7 8 9 10 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
<?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> |