Problems with php pg_query

100 Views Asked by At

I have a Problem with PHP. I want to make an table update with 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;");

Why does it not work?

Thanks!

1

There are 1 best solutions below

0
On

You do not have club_id defined in your code. And to avoid any problems and cleanup code I would do:

$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

It'll escape values for you so no need to handle strange cases.

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