查询失败错误:准备好的语句“ my_query”不存在

时间:2018-07-26 20:17:36

标签: php postgresql prepared-statement pg-query

大家好

我正在尝试通过PHP在Heroku中的PostgreSQL DB中插入表单数据,并且在这里尝试了所有解决方案,但是没有任何解决方案!我可以连接到数据库,但是没有任何操作对我有效! 此错误使我疯狂!

我的代码是:

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

 }


?>

1 个答案:

答案 0 :(得分:1)

第一行的代码中就有语法错误。

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

还有其他一些问题和奇怪之处,因此重写代码的某些部分会更容易。

我还建议您不要过多关注编码样式,缩进等。如果代码的样式正确,则将更容易阅读和帮助您。 PSR-2 Style Guide将是一个很好的起点。

所以这是重写的代码,但请注意,我没有安装PostgreSQL,这就是下面的代码未经任何方式测试的原因。它应该工作,但是也有可能不起作用。

有关详细说明,请参见代码中的注释。

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