1

I was trying to retrieve data from PostgreSQL database, In PHP.

Retrieving the data is fine but it does not work when it is linked with HTML.

When I click on find button in HTML interface, it did not really retrieve data from PostgrSQL.

Can anybody help?

PHP

$host        = "host=sv4gis";
$dbname      = "dbname=survey";
$credentials = "user=sde password=dfd54f";

$objectid = "";
$jobno = "";
$unit = "";
$coordinate = "";  
$name = "";
$northing = "";
$easting = "";
$elev = "";  

$db = pg_connect( "$host $dbname $credentials"  );
if(!$db){
    echo "Error : Unable to open database\n";
} else {
    echo "Opened database successfully\n";
}

function getPosts() {
    $posts = array();
    $posts[0] = $_POST['objectid'];
    $posts[1] = $_POST['jobno'];
    $posts[2] = $_POST['unit'];
    $posts[3] = $_POST['coordinate'];
    $posts[4] = $_POST['name'];
    $posts[5] = $_POST['northing'];
    $posts[6] = $_POST['easting'];
    $posts[7] = $_POST['elev'];

    return $posts;
}

// Selection Operation
if (isset($_POST['search'])) {
    $data = getPosts();
    $sql = "SELECT * FROM monu_from_Lst WHERE OBJECTID = $data[0]";
    $ret = pg_query($db, $sql);

    if($ret) {
        if(pg_num_rows($ret)) {
            while($row = pg_fetch_array($ret)) {
                echo    $objectid = $row['objectid'];
                echo     $jobno = $row['jobno'];
                echo    $unit = $row['unit'];
                echo     $coordinate = $row['coordinate'];
                echo    $name = $row['name'];
                echo     $northing = $row['northing'];
                echo    $easting = $row['easting'];
                echo     $elev = $row['elev'];
            }
            echo "Operation done successfully\n";
            // pg_close($db);

        }else{
            echo 'No Data For This Id';
        }
    } else {
        echo 'Result Error';
    }
}

HTML

<form action="php_insert_update_delete_search.php" method="post">
    <input type="number" name="objectid" placeholder="OBJECTID" value="<?php echo $objectid;?>">
    <input type="text" name="jobno" placeholder="JOB#" value="<?php echo $jobno;?>">
    <input type="text" name="unit" placeholder="UNIT" value="<?php echo $unit;?>">
    <input type="text" name="coordinate" placeholder="COORDINATE" value="<?php echo $coordinate;?>">
    <input type="text" name="name" placeholder="NAME" value="<?php echo $name;?>">
    <input type="number" name="northing" placeholder="NORTHING" value="<?php echo $northing;?>">
    <input type="number" name="northing" placeholder="NORTHING" value="<?php echo $northing;?>">
    <input type="number" name="easting" placeholder="EASTING" value="<?php echo $easting;?>">
    <input type="number" name="elev" placeholder="ElEVATION" value="<?php echo $elev;?>">
    <div>
        <input type="submit" name="search" value="Find">
    </div>
</form>

1 Answer 1

1

Your code seems to be fine. The problem might be with

<form action="php_insert_update_delete_search.php" method="post">

If you are posting the file to itself try using

<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">

Make sure your query runs successfully. if your query does not run successfully try following

$sql = "SELECT * FROM  \"monu_from_Lst\" WHERE objectid= $data[0]";

I have working code as following

<?php

$host        = "host=localhost";  // use your hostname
$dbname      = "dbname=blogsitedb"; // use your dbname
$credentials = "user=postgres password=admin"; // use your credentials

$objectid = "";
$jobno = "";
$unit = "";
$coordinate = "";  
$name = "";
$northing = "";
$easting = "";
$elev = "";  



$db = pg_connect("$host $dbname $credentials");

if(!$db)
{
    echo "Error : Unable to open database\n";
} 
else 
{
    echo "Opened database successfully\n";
}

function getPosts()
{
  $posts = array();
  $posts[0] = $_POST['objectid'];
  $posts[1] = $_POST['jobno'];
  $posts[2] = $_POST['unit'];
  /*    

  $posts[3] = $_POST['coordinate']; 
  $posts[4] = $_POST['name'];
  $posts[5] = $_POST['northing'];
  $posts[6] = $_POST['easting'];
  $posts[7] = $_POST['elev'];

  */

  return $posts;
}


// Selection Operation
if(isset($_POST['search']))
{
    $data = getPosts();
    $sql = "SELECT * FROM  \"monu_from_Lst\" WHERE objectid= $data[0]";

    $ret = pg_query($db, $sql);

     if($ret)
    {
        if(pg_num_rows($ret))
        {
            while($row = pg_fetch_array($ret)) 
            {
            echo    $objectid = $row['objectid'];
            echo     $jobno = $row['jobno'];
            echo    $unit = $row['unit'];
            /*echo     $coordinate = $row['coordinate'];
            echo    $name = $row['name'];
            echo     $northing = $row['northing'];
            echo    $easting = $row['easting'];
            echo     $elev = $row['elev'];*/
            }
             echo "Operation done successfully\n";
             // pg_close($db);

        }
        else
        {
            echo 'No Data For This Id';
        }
    }
    else
    {
        echo 'Result Error';
    }
}

?>

  <!DOCTYPE Html>
    <html>
        <head>
            <title>PHP INSERT UPDATE DELETE SEARCH</title>
        </head>
        <body>
            <form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
                <input type="number" name="objectid" placeholder="OBJECTID" value="<?php echo $objectid;?>"><br><br>
                <input type="text" name="jobno" placeholder="JOB#" value="<?php echo $jobno;?>"><br><br>
                <input type="text" name="unit" placeholder="UNIT" value="<?php echo $unit;?>"><br><br>
                <input type="text" name="coordinate" placeholder="COORDINATE" value="<?php echo $coordinate;?>"><br><br>
                 <input type="text" name="name" placeholder="NAME" value="<?php echo $name;?>"><br><br>
                 <input type="number" name="northing" placeholder="NORTHING" value="<?php echo $northing;?>"><br><br>
                <input type="number" name="northing" placeholder="NORTHING" value="<?php echo $northing;?>"><br><br>
                <input type="number" name="easting" placeholder="EASTING" value="<?php echo $easting;?>"><br><br>
                <input type="number" name="elev" placeholder="ElEVATION" value="<?php echo $elev;?>"><br><br>

                <div>

                    <!-- Input For Find Values With The given objectid -->

                     <input type="submit" name="search" value="Find">

                </div>
            </form>
        </body>
    </html>
Sign up to request clarification or add additional context in comments.

1 Comment

It worked if I use a specific query as below: $sql = "SELECT * FROM monu_from_Lst WHERE name= 'GPS15'"; However, when I was using $sql = "SELECT * FROM \"monu_from_Lst\" WHERE objectid= $data[0]"; It could not really find and iterate each database record, and the error is as below: Warning: pg_query(): Query failed: ERROR: syntax error at end of input LINE 1: SELECT * FROM "monu_from_Lst" WHERE objectid= ^ in C:\xampp\htdocs\test4.php on line 55 Result Error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.