How to perform insert with an argument and a query to database ?
By : Caroline
Date : May 03 2020, 08:19 AM
In my db I have a table like TABLE(user, match).
I'm trying to insert values in this table but I get a syntax error, I think I'm writing the query bad.
$query = "INSERT INTO table ($1, SELECT id FROM match_import) ON CONFLICT DO NOTHING; ";
$result = pg_prepare($dbh, "", $query);
$result = pg_execute($dbh, "", array($user));
The first value is an vriable passed with the pg_execute, but the second one (match) is taken from other table in my db.
I get this error
Error: Warning: pg_prepare(): Query failed: ERROR: syntax error at or near "$1" LINE 1: INSERT INTO table ($1, SELECT id FROM match_import) ON CON... ^ in /Applications/mappstack-7.1.27-3/apache2.4/htdocs...
How can I solve these issue?
You should try to add the argument to the query to database :
INSERT INTO table(column1, column2)
SELECT $1, id FROM match_import
ON CONFLICT DO NOTHING