-1

Hello All

I'm trying to insert a form data into my postgreSQL DB in heroku through PHP and i tried all the solutions here but nothing solved my problem!. I can connect to the database but no operation worked well to me!. This error lead me to craziness!.

my code is:

<?php


   $db_conn = pg_connect(" host="" port=5432 dbname="" user="" password="" ");

    if(!$db_conn){
             echo "Error : Unable to connect the database\n";
          } 


  if (isset($_POST['savedata'])) {


   $fn    =  $_POST ['fullname']; 
   $em    =  $_POST ['email']; 
   $ag    =  $_POST ['age'];
   $ge    =  $_POST ['gender'] ; 
   $ci    =  $_POST ['city'] ; 
   $de    =  $_POST ['degree']; 
   $ex    =  $_POST ['experience'];
   $jo    =  $_POST ['job']; 



  if($fn != "" and $em != "" and $ag != "" and $ge != "" and $ci != "" and $de != "" and $ex != "" and $jo != "") {


  $data1="something to test";



  $result = pg_prepare($db_conn, "my_query", "INSERT INTO members (fullname, email, age, gender, city, degree, experience, job) VALUES ($fn, $em, $ag, $ge, $ci, $de, $ex, $jo)");
  $result = pg_execute($db_conn, "my_query", array($data1));

  if (!$result){
    error_reporting(E_ALL);
    die("query failed".pg_last_error());
  }

  else {

  echo "<script>";
  echo "document.querySelector('.myalert').style.display = 'block';";
  echo "setTimeout(function(){
        document.querySelector('.myalert').style.display = 'none';
        window.location.replace('home');
      },5000);"; 
  echo "</script>";

 }

  }

  else {
    echo "<script>";
    echo "document.querySelector('.myalert1').style.display = 'block';";
    echo "setTimeout(function(){
        document.querySelector('.myalert1').style.display = 'none';
      },2000);"; 
    echo "</script>";
  }

 }


?>
Ali Barani
  • 238
  • 2
  • 7
  • 17
  • 1
    You have a quoting issue in your very first line – Patrick Q Jul 26 '18 at 20:20
  • Tell me exactly in which line the quotation issue? – Ali Barani Jul 26 '18 at 20:22
  • From looking at the [examples](http://php.net/manual/en/function.pg-prepare.php), you should use single quotes when preparing your query instead of double quotes. Double quotes will expand your variables, but you instead want to leave the actual variable names in. – aynber Jul 26 '18 at 20:22
  • Look at the highlighting in the code here. The quotes in your `pg_connect` statement are throwing things off. – aynber Jul 26 '18 at 20:23
  • Ok i will try to surround them by a single ' ' – Ali Barani Jul 26 '18 at 20:24
  • I think you also need to change your variable names in your prepare query. `If any parameters are used, they are referred to in the query as $1, $2, etc.` – aynber Jul 26 '18 at 20:25
  • @aynber Don't focus on the missing " in the end of pg_connect function because i forgot to put here but it is already exist in my code, so i will edit the post now. – Ali Barani Jul 26 '18 at 20:26
  • How i can do that @aynber? – Ali Barani Jul 26 '18 at 20:28
  • Please do not re-type your code here by hand. Instead, copy/paste the exact code whenever possible. – Patrick Q Jul 26 '18 at 20:29
  • @PatrickQ Yes i did that copy/paste but only forgot " in the end of pg_connect(). I will try your solution with a single ' ' and give you the results ok. – Ali Barani Jul 26 '18 at 20:31
  • @PatrickQ i got the same error :( – Ali Barani Jul 26 '18 at 20:33
  • @PatrickQ Check it from here https://thegate2018.herokuapp.com/ – Ali Barani Jul 26 '18 at 20:34
  • See [aynber's comment](https://stackoverflow.com/questions/51546878/query-failederror-prepared-statement-my-query-does-not-exist#comment90061716_51546878). Your call to `pg_prepare()` is likely failing. You should be checking the result of that call before you execute. – Patrick Q Jul 26 '18 at 20:37
  • @PatrickQ Can you give me the proper code to solve this or not? – Ali Barani Jul 26 '18 at 20:42
  • Not with that kind of attitude, no. Goodbye. – Patrick Q Jul 26 '18 at 20:44

1 Answers1

1

You have syntax error in your code at the very first line.

Parse error: syntax error, unexpected '" port=5432 dbname="' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ')

There are also some other issues and oddities so it was just easier to rewrite some parts of your code.

I would also advice you to pay little more attention about the coding style, indentations and etc. It would be significantly easier to read and help you if the code were styled properly. PSR-2 Style Guide would be good place to start.

So here's the rewritten code, but note that I don't have PostgreSQL installed and that's why the code below isn't tested in any way. It should work, but there's also possibility that it doesn't.

See the comments in the code for further explanation.

// Change these credentials according to your needs, but without any quotes
$db_conn = pg_connect("host=localhost port=5432 dbname=mydb user=user password=pwd");

if (!$db_conn) {
    die("Error : Unable to connect the database\n");
}

if (isset($_POST['savedata'])) {
    $member_details = array(
        $_POST['fullname'],
        $_POST['email'],
        $_POST['age'],
        $_POST['gender'],
        $_POST['city'],
        $_POST['degree'],
        $_POST['experience'],
        $_POST['job']
    );
}

// Iterates through the array and checks if there's any items of which has no proper value
foreach ($member_details as $key => $val) {
    if (empty($val)) {
        die($key . ' is empty.');
    }
}

// Query inside single quotes, also variable names must be $1, $2, $3 etc
$query = 'INSERT INTO members (fullname, email, age, gender, city, degree, experience, job) VALUES ($1, $2, $3, $4, $5, $5, $7, $8)';

$result = pg_prepare($db_conn, "my_query", $query);
$result = pg_execute($db_conn, "my_query", $member_details);

if (!$result) {
    // error actions
} else {
    // success actions
}
cannon
  • 61
  • 2