bennius
bennius

Reputation: 121

How to perform an insert with an argument and a query

In my database I have a table TABLE(person, match). I'm trying to insert values in this table but I get a sintax error, I think I'm writing the query wrong.

   $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 argument passed with the pg_execute, but the second one (match) is taken from another table in the database.

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-2/apache2/htdocs...

How can I solve these problem?

Upvotes: 1

Views: 104

Answers (1)

clemens
clemens

Reputation: 17721

You should add the argument to the query:

INSERT INTO table(column1, column2)
SELECT $1, id FROM match_import
ON CONFLICT DO NOTHING

Upvotes: 1

Related Questions