How to perform an insert with an argument and a query - php

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?

You should add the argument to the query:
INSERT INTO table(column1, column2)
SELECT $1, id FROM match_import
ON CONFLICT DO NOTHING

Related

PostgreSQL not allowed the string values

The following PostgreSQL query producing me the following error. But in my intuition everything is correct. I don't know why PostgreSQL making it wrong.
$query = "INSERT INTO test (sku) VALUES ("."3814TT82033102-2".")";
The sku filed is VARCHAR(50) but why the postgres query not allowed my sku. I got the following
PHP Warning: pg_exec(): Query failed: ERROR: syntax error at or near "TT82033102"
LINE 1: INSERT INTO test (sku) VALUES (3814TT82033102-2)
^ in /home/nifras/Documents/User/working-dir/csv/functions/import-rules.php on line 11
I'm not a php expert, but I feel a bind parameter could be used.
$sku = '3814TT82033102-2';
$query = 'INSERT test (sku) VALUES VALUES(:sku)';
$stmt = $this->pdo->prepare($query);
$stmt->bindValue(':sku', $sku);
I have changed my query as follow
$product_sku = $product[0];
$query = "INSERT INTO test (sku) VALUES ('$product_sku')";
The issue is strings should cover by singlw quotes
Thanks a_horse_with_no_name

PHP: Error on Update statement with subquery

I have a page that updates the data of a specific user. The user has position, which is a foreign key. The query update (below) works fine without the position, but with the position I get the following error.
Query :
$queryUpdate = "UPDATE visitorsystem.employee SET idNumber = '$idNumber', name = '$name',
surname = '$surname',
position = 'SELECT positionid FROM visitorsystem.position WHERE positionName LIKE '%$position%'',
email = '$email'
WHERE employeeid = '$empId'";
$resultUpdate = mysqli_query($connection,$queryUpdate)
or die("Error in query: ". mysqli_error($connection));
Error in query: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near 'SELECT positionid FROM visitorsystem.position WHERE
positionName LIKE '%Informat' at line 3
I have tried to work my way around by using inner join as I have seen some solutions given here on stack but nothing has worked. Any Suggestions ?
Subqueries go within regular parens, not quotes, so in a general sense:
SELECT x FROM y WHERE z IN (SELECT z FROM a)
Single and double quotes (by default) are only for string values.

Insert Into with Select(PDO) and php

Ok so I need to insert some values into a table but i need to search for one of the data in the database so i tried to do it like this
$bookid = $_GET['var'];
$username = $_GET['username'];
$quer2 = "Insert Into reserved (username,bookid) VALUES ((SELECT userid FROM users WHERE username=:username),:var)";
$query2 = $dbc->prepare($quer2);
$query2 ->bindParam(':username',$username);
$query2 ->bindParam(':var',$bookid);
$query2 ->execute();
The problem is that I get this error
SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "username" violates not-null constraint DETAIL: Failing row contains (6, null, 2014-09-22 13:06:33.262).
I tried the query in the database and it works so I guess that there is some error in bindParam but I can't understand what it is... So if anyone could help me I would appreciate it, thank you
change
$query2 ->bindParam(':var',$var);
to
$query2 ->bindParam(':var',$bookid);
update your query to something like this
Insert Into reserved (username,bookid)
SELECT userid, :var FROM users WHERE username=:username
just bind the params and run the query.

Query getting executed but shows error

$sql="INSERT INTO $p (q,o1,o2,o3,o4,ta,ma) VALUES ('$q','$o1','$o2','$o3','$o4','$ta','$ma')";
this query is getting executed but shows error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near '(q,o1,o2,o3,o4,ta,ma) VALUES
('','','','','','','')' at line 1
any idea??
use this query
$sql=" INSERT INTO '$p' ( `q` , `o1` ,`o2` ,`o3` , `o4` ,`ta` ,`ma`) VALUES ('$q','$o1','$o2','$o3','$o4','$ta','$ma') "
use single quotes for '$p'
$sql="INSERT INTO '$p' (`q`,`o1`,`o2`,`o3`,`o4`,`ta`,`ma`) VALUES ('$q','$o1','$o2','$o3','$o4','$ta','$ma')";
Always use single comma on php variable if you are using double comma at the start. Like this
$query = "INSERT INTO 'table' WHERE 'user' = '$user'";
in your case:
" INSERT INTO '$p' ( `q` , `o1` ,`o2` ,`o3` , `o4` ,`ta` ,`ma`) VALUES ('$q','$o1','$o2','$o3','$o4','$ta','$ma') "

cant insert data into postgresDB in php

im having trouble inserting my data's from textbox into postgresdb.
my insert into tbl_ingredients is working fine but my insert into tbl_item is having a troubles can't figure it out how and where?
Connect();
$sql="INSERT INTO tbl_item VALUES('$itemname', '$highthreshold', '$lowthreshold', '$Qpunit', '$description', '$date');";
$iteminfo = pg_query($sql);
$sql1="SELECT MAX(itemid) as newid FROM tbl_item;";
$iden_new = pg_query($sql1);
$fetched_row = pg_fetch_row($iden_new,NULL,PGSQL_BOTH);
$newid=$fetched_row['newid'];
$sql2="INSERT INTO tbl_ingredient VALUES('$newid', '$Brandname');";
$ingredients = pg_query($sql2);
CloseDB();
if(!$sql)
{
$sucmsg = "Successfully added new Item, ".ucfirst($itemname)."!";
echo $sucmsg;
}
else
{
echo "error in saving data";
}
table structure:
tbl_item
itemid>itemname>highquantitythreshold>lowquantitythreshold>qntyperunit>Itemtype>description>dateadded
tbl_ingredient
itemid>brandname
im getting wamp "Warning: pg_query(): Query failed: ERROR: invalid input syntax for integer: "Strawberry" LINE 1: INSERT INTO tbl_item VALUES('Strawberry', '6', '3', '1300gra... ^ in D:\Wamp\wamp\www\Php\CTea\AddItem.php on line 247"
can someone lend me a helping hand thanks!.
You either need to use NULL ->
$sql="INSERT INTO tbl_item VALUES(NULL, '$itemname', '$highthreshold', '$lowthreshold', '$Qpunit', '$description', '$date');";
OR
You need to specify the columns you are inserting into ->
$sql="INSERT INTO tbl_item (itemname, highquantitythreshold, lowquantitythreshold, qntyperunit, description, dateadded) VALUES('$itemname', '$highthreshold', '$lowthreshold', '$Qpunit', '$description', '$date');";
Without the NULL or column name, the database does not know that you are skipping the first column - itemid - so it will try to insert the 1st value into that column.
from the manual - http://www.postgresql.org/docs/9.1/static/sql-insert.html
The target column names can be listed in any order. If no list of
column names is given at all, the default is all the columns of the
table in their declared order; or the first N column names, if there
are only N columns supplied by the VALUES clause or query. The values
supplied by the VALUES clause or query are associated with the
explicit or implicit column list left-to-right.
Each column not present in the explicit or implicit column list will
be filled with a default value, either its declared default value or
null if there is none.

Categories