• Thu, Nov 2024

How to insert data using PostgreSQL and PHP

How to insert data using PostgreSQL and PHP

This tutorial uses the test_db and employee table names from the PostgreSQL database.

We will learn how to enter data using PHP and PostgreSQL in this tutorial.

This tutorial's file structure

config.php: This is used to connect PHP to PostgreSQL databases.

index.php: This is where HTML forms are used, and PHP code for data input is also placed.

This tutorial uses the test_db and employee table names from the PostgreSQL database.

Prior to executing this PostgreSQL Table structure script, establish the database (test_db ).

CREATE TABLE IF NOT EXISTS public.employee
(
   id integer NOT NULL DEFAULT nextval('tblemployee_id_seq'::regclass),
   name character varying(255) COLLATE pg_catalog."default",
   emailid character varying(255) COLLATE pg_catalog."default",
   mobileno integer,
   department character varying(255) COLLATE pg_catalog."default",
   creationdate date NOT NULL DEFAULT CURRENT_DATE,
   CONSTRAINT employee_pkey PRIMARY KEY (id)
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.tblemployee
   OWNER to postgres;

Code for PostgreSQL connection with PHP (config.php)

<?php
// Database configuration
$host = 'localhost';
$db   = 'test_db ';  
$user = 'postgres';
$pass = 'Test@123'; 
$port = '5432'; // Default port for PostgreSQL

// Create connection string
$connection_string = "host=$host port=$port dbname=$db user=$user password=$pass";

// Establish a connection to the PostgreSQL database
$conn = pg_connect($connection_string );

if (!$conn) {
   echo "Error: Unable to open database\n";
   exit;
}

Create an HTML Form for user inputs

<div class="signup-form">
   <form method="post" class="form-horizontal">
         <div class="row">
           <div class="col-8 offset-4">
                <h2>Sign Up</h2>
            </div>    
         </div>            
       <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="name" 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="email" 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="mobile" 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="dept" required="required">
           </div>            
       </div>
        <div class="form-group row">
            <div class="col-8 offset-4">

                <button type="submit" name="submit" class="btn btn-primary btn-lg">Submit</button>
            </div>  
        </div>              
   </form>
</div>

Now get user inputs and store them in the PHP variables

$ename=$_POST['name'];
$eemail=$_POST['email'];
$emobile=$_POST['mobile'];
$edept=$_POST['dept'];

Query for inserting data into PostgreSQL using PHP

$result = pg_query($conn, "insert into employee(name,emailid,mobileno,department) values('$ename','$eemail','$emobile','$edept')");

if ($result) {
   echo "Data inserted successfully!";
} else {
   echo "Error: " . pg_last_error($conn);
}

Full PHP Script for inserting data into the database PostgreSQL 

<?php include_once('config.php');
if(isset($_POST['submit']))
{
$ename=$_POST['pname'];
$eemail=$_POST['email'];
$emobile=$_POST['mobile'];
$edept=$_POST['dept'];

// Execute the query with parameters
$result = pg_query($conn, "insert into employee(name,emailid,mobileno,department) values('$ename','$eemail','$emobile','$edept')");

if ($result) {
   echo "Data inserted successfully!";
} else {
   echo "Error: " . pg_last_error($conn);
}
// Close the connection
pg_close($conn);
}
?>