PHP pg_query update statement

1.2k Views Asked by At

I am trying to updata a database table using pq_query in PHP. I have the following code:

$q = "UPDATE tableName SET ('data1 = " . $data1 . "', data2='" . $data2 . "') WHERE user=".$user;
$success = pg_query($q); 
if (!$success) { 
    $errormessage = pg_last_error(); 
    echo "Error " . $errormessage; 
}

I am getting the following error message:

ERROR: syntax error at or near "'data1 = '"

LINE 1: UPDATE tableName SET ('data1 = 10', data2= 20'') WHERE user=

2

There are 2 best solutions below

1
On BEST ANSWER

Replace your query with this query

$q = "UPDATE tableName SET data1 = '$data1', data2='$data2' WHERE user='$user'";

Explaination: You should pass variable in single quotes('') if your query in double quotes.

1
On

You are using a lot of quotes which it is not understood by PostgreSQL, try simply this :

$q = "UPDATE tableName SET data1 = " . $data1 . ", data2=" . $data2 . " WHERE user=".$user;

Remove those single quotes !