I have this piece of code in PHP and using a PostgreSQL as the database. I am getting all the parameters from the GET. Have checked them by printing it. The formed query executes on a Postgres terminal but fails from the PHP script.
Here is the piece of code.
<?php
$link = pg_connect("host=localhost dbname=postgres user=postgres password=password") or die('connection failed');
# Building the query
$newq=sprintf("update purchase_info set ....... comments=%s where id=%s",......,$comm,$id);
print $newq; // This query runs on the postgres terminal
$query=addslashes($newq); // to escape "" as one of my fields is comments
$result=pg_query($link,$newq);
if (!$result) {
echo "An error occured.\n";
}
pg_close($link);
?>
Other queries run in the same script. This SQL statement has about 14 field being updated.
What Is going wrong hear.
Appreciate the help!
You shouldn’t be using
addslashesto quote strings for PostgreSQL, you should usepg_escape_literal:You should never use
addslashesfor quoting strings for a database:You should be doing this:
I’m assuming that
idis actually a number as well.