Problems with php pg_query



我对PHP有问题。我想用 PHP 更新表。

    $name = pg_escape_string($_POST['NAME']);
    $place = pg_escape_string($_POST['PLACE']);
    $zip = pg_escape_string($_POST['ZIP']);
    $nation = pg_escape_string($_POST['NATION']);
    $name = "'"  .  $name . "'";
    $place = "'"  .  $place . "'";
    $zip = "'"  .  $zip . "'";
    $nation = "'"  .  $nation . "'";
    $club_id = "'"  .  $club_id . "'";        

   $result = pg_query($db_connect, "UPDATE club SET name_c = $name, place_c = $place, zip_c = $zip, nation_c = $nation WHERE id_c = $club_id;");

为什么它不起作用?

谢谢!

代码中没有定义club_id。为了避免任何问题和清理代码,我会这样做:

$club_id = 1;
$dbconn = pg_connect("connectionstring");
$sql = 'UPDATE club SET name_c = $1, place_c = $2, zip_c = $3, nation_c = $4 WHERE id_c = $5;';
$result = pg_query_params($dbconn, $sql, array(
    $_POST['NAME'],
    $_POST['PLACE'],
    $_POST['ZIP'],
    $_POST['NATION'],
    $club_id
));
// Do what you need

它会为您转义值,因此无需处理奇怪的情况。

http://php.net/manual/en/function.pg-query-params.php

相关内容

  • 没有找到相关文章

最新更新