1

I am trying to send the data from javascript to Postgresql using PHP however I am facing a problem. I don't know if my PHP connects to the Postgres at all. I get no errors or any changes in the database.

js function
function sendData(Firstname, Surname, Age) {

    const hr = new XMLHttpRequest();
    const url = "post.php";
    const data = "Firstname=" + Firstname + "&Surname=" + Surname + "&Age=" + Age;
    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    // Send the data to PHP now... and wait for response to update the status div
    hr.send(data); // Actually execute the request
}
window.onload = function () {
    sendData("rob", "roy", "23");
};

and post.php

<?php 

$First = pg_escape_string($_POST['Firstname']);
$Last =  pg_escape_string($_POST['Surname']);
$Age = pg_escape_string($_POST['Age']);

$host        = "host = localhost";
$port        = "port = 5432";
$dbname      = "dbname = postgres";
$credentials = "user = postgres password=root";

// Connecting, selecting database
$dbconn = pg_connect("$host $port $dbname $credentials")
if($dbconn){
    echo "Connected";
}
    else{
     die('Could not connect: ' . pg_last_error());
    }
                        //tried only people doesn't work
$dbquery = "INSERT INTO public.people(Firstname, Surname, Age) VALUES('$First','$Last','$Age')";
$result = pg_query($dbconn, $dbquery);

       if (!$result) { 
            $errormessage = pg_last_error(); 
            echo "Error with query: " . $errormessage; 
            exit(); 
        }  
echo "Operation done successfully\n";
// Closing connection
pg_close($dbconn);
?>

Database

Patrick Q
  • 6,373
  • 2
  • 25
  • 34
Hina Satti
  • 55
  • 6
  • What do you get if you `var_dump($dbconn);`? If you are _not_ getting connected, the first change I would suggest is removing the spaces in your strings like "host = localhost"; – Patrick Q Dec 03 '19 at 18:38
  • Hi i tried ```var_dump($dbconn);``` and removed spaces between strings still no response. – Hina Satti Dec 03 '19 at 18:47
  • You also may want to consider using [prepared statements](https://www.php.net/manual/en/function.pg-prepare.php) so that you don't have to worry about quoting issues and SQL injection – Patrick Q Dec 03 '19 at 18:47
  • What exactly do you mean by "no response"? The `var_dump()` would always show you _something_. Where are you _looking_ for the response? – Patrick Q Dec 03 '19 at 18:48
  • I am using var_dump() just after declaring $dbconn. Thanks for the recommendation. – Hina Satti Dec 03 '19 at 18:52
  • Okay, but you didn't answer my question – Patrick Q Dec 03 '19 at 18:52
  • Just so we on the same page, i need to look for var_dump() -> response in the console, right? – Hina Satti Dec 03 '19 at 18:57
  • Yes. You already claimed that you were getting no response though. So that's why I asked where you were _looking_ for a response. Were you not? – Patrick Q Dec 03 '19 at 18:58
  • yes, i was but i didn't get using ```var_dump($dbconn)``` does that mean the connection is not established right? – Hina Satti Dec 03 '19 at 19:00
  • As I said, `var_dump()` will always output _something_, even if it is `NULL`. So if you are literally getting _no_ response, then you are either looking in the wrong place, or something is breaking your code _before_ that. – Patrick Q Dec 03 '19 at 19:04
  • I don't know what's wrong btw im looking in firefox console also looked in chrome console. – Hina Satti Dec 03 '19 at 19:13
  • [See Here](https://stackoverflow.com/questions/21533285/why-the-ajax-script-is-not-running-on-iis-7-5-win-2008-r2-server/21617685#21617685). Note that steps 4 and 5 should be adjusted for your specific case, but otherwise, that's how you should be looking for the response. – Patrick Q Dec 03 '19 at 19:22
  • Thanks this was helpful. The data is been sent but its been blocked from somewhere. Timing tab in the firefox debugger shows waiting for a response. – Hina Satti Dec 03 '19 at 19:53

0 Answers0