Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 16, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 16, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 16, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 16, 2025

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025

      Minecraft licensing robbed us of this controversial NFL schedule release video

      May 16, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The power of generators

      May 16, 2025
      Recent

      The power of generators

      May 16, 2025

      Simplify Factory Associations with Laravel’s UseFactory Attribute

      May 16, 2025

      This Week in Laravel: React Native, PhpStorm Junie, and more

      May 16, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025
      Recent

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»How to update the Data in PostgreSQL using PHP

    How to update the Data in PostgreSQL using PHP

    November 15, 2024

    In this tutorial, we will learn how to update the record or data in PostgreSQL using PHP.

    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.

    edit.php: This file is sued to update the particular record on the basis of the row id.

    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;
    }

    Read / Fetch the particular record (read.php)

    create read.php file. For fetching a record we have to get the row id of that record and store in $empid. We access the $_GET[‘id’] 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="edit.php?id=<?php echo $row['id'];?>" class="btn btn-info btn-sm">Edit</td>
                        </tr>
                    <?php $cnt++;} ?>
                         
                    </tbody>
                </table>

    Now Fetch the data in the HTML Form.

    create edit.php file. For updating a record we have to get the row id of that record and store in $empid. We access the $_GET[‘id’] variable to do it.
    Code for gets a record based on the given id. Through this way, we can get data autofill-data in HTML Form.

    <form method="post" class="form-horizontal">
    <?php 
    $empid=$_GET['id'];
    $query= pg_query($conn,"select * from tblemployee where id='$empid'");
    $cnt=1;
    while($row=pg_fetch_array($query)){
    ?>
    
    
            <div class="form-group row">
    	<label class="col-form-label col-4">Name</label>
    	<div class="col-8">
           <input type="text" class="form-control" name="empname" value="<?php echo $row['empname'];?>" required="required">
                </div>        	
            </div>
    		
    <div class="form-group row">
    <label class="col-form-label col-4">Email</label>
    <div class="col-8">
    <input type="email" class="form-control" name="empemail" value="<?php echo $row['empemailid'];?>" required="required">
                </div>        	
            </div>
    		
    <div class="form-group row">
    <label class="col-form-label col-4">Mobile</label>
    <div class="col-8">
     <input type="text" class="form-control" name="empmobile" value="<?php echo $row['empmobileno'];?>" required="required">
              </div>        	
            </div>
    		
    <div class="form-group row">
    <label class="col-form-label col-4">Department</label>
    <div class="col-8">
    <input type="text" class="form-control" name="empdept" value="<?php echo $row['empdepartment'];?>" required="required">
                </div>        	
            </div>
    
    <div class="form-group row">
    <label class="col-form-label col-4">Creation Date</label>
    <div class="col-8">
     <input type="text" class="form-control" name="cdate" value="<?php echo $row['creationdate'];?>" readonly>
      </div>        	
       </div>
       <?php } ?>
    		
    <div class="form-group row">
    <div class="col-8 offset-4">
    <button type="submit" name="update" class="btn btn-primary btn-lg">Update</button>
    </div>  
    </div>	
     </form>

    Code for update the particular record. Put this code on the top of the edit.php

    <?php include_once('dbcon.php');
    if(isset($_POST['update']))
    {
    $ename=$_POST['empname'];
    $eemail=$_POST['empemail'];
    $emobile=$_POST['empmobile'];
    $edept=$_POST['empdept'];
    $empid=$_GET['id'];
    // Execute the query with parameters
    $result = pg_query($conn, "update tblemployee set empname='$ename',empemailid='$eemail',empmobileno='$emobile',empdepartment='$edept' where id='$empid'");
    
    if ($result) {
         echo '<script>alert("Employee Details updated successfully!")</script>';
        echo "<script type='text/javascript'> document.location = 'read.php'; </script>";
    } else {
        echo "Error: " . pg_last_error($conn);
    }
    // Close the connection
    pg_close($conn);
    }
    ?>

    Here is the full code written in the edit.php

    <?php include_once('dbcon.php');
    if(isset($_POST['update']))
    {
    $ename=$_POST['empname'];
    $eemail=$_POST['empemail'];
    $emobile=$_POST['empmobile'];
    $edept=$_POST['empdept'];
    $empid=$_GET['id'];
    // Execute the query with parameters
    $result = pg_query($conn, "update tblemployee set empname='$ename',empemailid='$eemail',empmobileno='$emobile',empdepartment='$edept' where id='$empid'");
    
    if ($result) {
         echo '<script>alert("Employee Details updated successfully!")</script>';
        echo "<script type='text/javascript'> document.location = 'read.php'; </script>";
    } else {
        echo "Error: " . pg_last_error($conn);
    }
    // Close the connection
    pg_close($conn);
    }
    ?>
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:400,700">
    <title>Data Updation  in PostgreSQL usinh PHP</title>
    <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: #999;
    	background: #f3f3f3;
    	font-family: 'Roboto', sans-serif;
    }
    .form-control {
    	border-color: #eee;
    	min-height: 41px;
    	box-shadow: none !important;
    }
    .form-control:focus {
    	border-color: #5cd3b4;
    }
    .form-control, .btn {        
    	border-radius: 3px;
    }
    .signup-form {
    	width: 500px;
    	margin: 0 auto;
    	padding: 30px 0;
    }
    .signup-form h2 {
    	color: #333;
    	margin: 0 0 30px 0;
    	display: inline-block;
    	padding: 0 30px 10px 0;
    	border-bottom: 3px solid #5cd3b4;
    }
    .signup-form form {
    	color: #999;
    	border-radius: 3px;
    	margin-bottom: 15px;
    	background: #fff;
    	box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);
    	padding: 30px;
    }
    .signup-form .form-group row {
    	margin-bottom: 20px;
    }
    .signup-form label {
    	font-weight: normal;
    	font-size: 14px;
    	line-height: 2;
    }
    .signup-form input[type="checkbox"] {
    	position: relative;
    	top: 1px;
    }
    .signup-form .btn {        
    	font-size: 16px;
    	font-weight: bold;
    	background: #5cd3b4;
    	border: none;
    	margin-top: 20px;
    	min-width: 140px;
    }
    .signup-form .btn:hover, .signup-form .btn:focus {
    	background: #41cba9;
    	outline: none !important;
    }
    .signup-form a {
    	color: #5cd3b4;
    	text-decoration: underline;
    }
    .signup-form a:hover {
    	text-decoration: none;
    }
    .signup-form form a {
    	color: #5cd3b4;
    	text-decoration: none;
    }	
    .signup-form form a:hover {
    	text-decoration: underline;
    }
    </style>
    </head>
    <body>
    <div class="signup-form">
    
          	<div class="row">
            	<div class="col-12">
    				<h2>Update Employee Data</h2>
    			</div>	
          	</div>			
        <form method="post" class="form-horizontal">
    <?php 
    $empid=$_GET['id'];
    $query= pg_query($conn,"select * from tblemployee where id='$empid'");
    $cnt=1;
    while($row=pg_fetch_array($query)){
    ?>
    
    
            <div class="form-group row">
    			<label class="col-form-label col-4">Name</label>
    			<div class="col-8">
                    <input type="text" class="form-control" name="empname" value="<?php echo $row['empname'];?>" required="required">
                </div>        	
            </div>
    		<div class="form-group row">
    			<label class="col-form-label col-4">Email</label>
    			<div class="col-8">
                    <input type="email" class="form-control" name="empemail" value="<?php echo $row['empemailid'];?>" required="required">
                </div>        	
            </div>
    		<div class="form-group row">
    			<label class="col-form-label col-4">Mobile</label>
    			<div class="col-8">
                    <input type="text" class="form-control" name="empmobile" value="<?php echo $row['empmobileno'];?>" required="required">
                </div>        	
            </div>
    		<div class="form-group row">
    			<label class="col-form-label col-4">Department</label>
    			<div class="col-8">
                    <input type="text" class="form-control" name="empdept" value="<?php echo $row['empdepartment'];?>" required="required">
                </div>        	
            </div>
    
            	<div class="form-group row">
    			<label class="col-form-label col-4">Creation Date</label>
    			<div class="col-8">
                    <input type="text" class="form-control" name="cdate" value="<?php echo $row['creationdate'];?>" readonly>
                </div>        	
            </div>
        <?php } ?>
    		<div class="form-group row">
    			<div class="col-8 offset-4">
    
    				<button type="submit" name="update" class="btn btn-primary btn-lg">Update</button>
    			</div>  
    		</div>	
     </form>
    		<div class="form-group row">
    			<div class="col-8">
        <a href="read.php" style="color:red">View Data</a>
                </div>        	
            </div>
    
    
    </div>
    </body>
    </html>
    PHP-PostgreSQL Data Update/Edit Script (Download Source Code)
    Size: 314 KB
    Version: V 1.0
    Download Now!

    The post How to update the Data in PostgreSQL using PHP appeared first on PHPGurukul.

    Source: Read More 

    Hostinger
    Facebook Twitter Reddit Email Copy Link
    Previous ArticleHow to delete data from PostgreSQL using PHP
    Next Article Ensuring a durable transition

    Related Posts

    Machine Learning

    Salesforce AI Releases BLIP3-o: A Fully Open-Source Unified Multimodal Model Built with CLIP Embeddings and Flow Matching for Image Understanding and Generation

    May 16, 2025
    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 16, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Development Release: Elive 3.8.48 (Beta)

    News & Updates

    Critical Vulnerabilities in Browser Wallets Let Attackers Drain your Funds

    Security

    openapi-tui lists, browses and runs APIs

    Linux

    Chinese APT Gelsemium Targets Linux Systems with New WolfsBane Backdoor

    Development

    Highlights

    CVE-2025-4589 – WordPress Bon Toolkit Stored Cross-Site Scripting Vulnerability

    May 15, 2025

    CVE ID : CVE-2025-4589

    Published : May 15, 2025, 4:16 a.m. | 39 minutes ago

    Description : The Bon Toolkit plugin for WordPress is vulnerable to Stored Cross-Site Scripting via the plugin’s ‘bt-map’ shortcode in all versions up to, and including, 1.3.2 due to insufficient input sanitization and output escaping on user supplied attributes. This makes it possible for authenticated attackers, with contributor-level access and above, to inject arbitrary web scripts in pages that will execute whenever a user accesses an injected page.

    Severity: 6.4 | MEDIUM

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    CVE-2025-3521 – “WordPress Team Members Stored Cross-Site Scripting”

    May 1, 2025

    Samsung’s 500Hz gaming monitor isn’t just wicked fast — it overcomes a big OLED flaw

    May 13, 2025

    EcoFlow’s new backyard solar energy system starts at $599 – no installation crews or permits needed

    May 9, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.