php pg_query的问题

时间:2016-03-01 22:06:25

标签: php postgresql 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;");

为什么不起作用?

谢谢!

1 个答案:

答案 0 :(得分:1)

您的代码中未定义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