I am trying to use this statement to insert new data into my orders table like so:
$tsql2 =
"INSERT INTO pedidos (numero_local, emailUser,codigopapel,cantidad, color)
VALUES ('$local', '$emailOn', '$codigopapel', '$cantidad', '$selectcolor')";
$stmt2 = sqlsrv_query($conn, $tsql2);
I have foreign keys on this table which are:
- numero_local(location number) which comes from a location table
- emailUser comes from a user table
- codigopapel(paper code) which comes from a table called paper
These are my 3 foreign keys and I have tried doing the INSERT as shown above and using:
INSERT INTO pedidos VALUES((SELECT numerolocal FROM localidad WHERE numerolocal = $local))
The above is an example but I have written the SELECTS for all foreign key variables and yet the insert does not work.
Finally I have checked that the data types for all foreign keys and the data being uploaded are consistent across tables and coming from the web page.
In short: What am I doing wrong?
die( print_r( sqlsrv_errors(), true));
in your script if the query fails, to get the error message(s). Also stop building your queries like that. Use prepared statemnts.