-1

大家好

我正在尝试通过 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>";
  }

 }


?>
4

1 回答 1

1

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

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

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

我还建议您多注意编码样式、缩进等。如果代码样式正确,它将更容易阅读并为您提供帮助。PSR-2 样式指南将是一个很好的起点。

所以这是重写的代码,但请注意我没有安装 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
}
于 2018-07-26T22:12:44.110 回答