0

I can't find the error after 3 hours trying. Can someone help my ?

this is my query =

$query = "INSERT INTO `card`(ProductName,ProductBarcode,ProductPakking,ProductPresent,ProductMinimuim,FotoUrl,DBid) SELECT * FROM (SELECT '" . $ProductName . "','" . $ProductBarcode . "','" . $ProductPakking . "','" . $ProductPresent . "','" . $ProductMinimuim . "','" . $FotoUrl . "','" . $DBid ."') AS tmp WHERE NOT EXISTS (SELECT name FROM `card` WHERE `ProductName` = " . $ProductName . ",`ProductBarcode` = " . $ProductBarcode . ",`ProductPakking` = " . $ProductPakking . ",`ProductPresent` = " . $ProductPresent . ",`ProductMinimuim` = " . $ProductMinimuim . ",`FotoUrl` = " . $FotoUrl . ",`DBid` = " . $DBid ." ) LIMIT 1;";

This is when I echo my query =

INSERT INTO `card`(ProductName,ProductBarcode,ProductPakking,ProductPresent,ProductMinimuim,FotoUrl,DBid)
SELECT * FROM (
    SELECT 'brood','8717333541583','zak','-1937','11','img/img.svg','3') AS tmp
WHERE NOT EXISTS (
    SELECT name FROM `card` WHERE
        `ProductName` = 'brood',
        `ProductBarcode` = '8717333541583',
        `ProductPakking` = 'zak',
        `ProductPresent` = '-1937',
        `ProductMinimuim` = '11',
        `FotoUrl` = 'img/img.svg',
        `DBid` = '3' )
) LIMIT 1

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 'ProductBarcode = ,ProductPakking = ,ProductPresent = ,ProductMinimuim = ' at line 1

johndeer
  • 41
  • 8

1 Answers1

3

I'm assuming $ProductName is not an integer, so you'd need to include that in quotes here:

WHERE `ProductName` = '" . $ProductName . "'

Also, please note, you SHOULD NOT be concatenating variables into a string like that. Please see this post for info about how bad this is.

An updated SQL statement PLEASE DON'T USE THIS, BIND VARIABLES INSTEAD:

INSERT INTO `card`(ProductName,ProductBarcode,ProductPakking,ProductPresent,ProductMinimuim,FotoUrl,DBid)
SELECT * FROM (
    SELECT '" . $ProductName . "','" . $ProductBarcode . "','" . $ProductPakking . "','" . $ProductPresent . "','" . $ProductMinimuim . "','" . $FotoUrl . "','" . $DBid ."') AS tmp
WHERE NOT EXISTS (
    SELECT ProductName FROM `card` WHERE
        `ProductName` = '" . $ProductName . "' AND
        `ProductBarcode` = '" . $ProductBarcode . "' AND
        `ProductPakking` = '" . $ProductPakking . "' AND
        `ProductPresent` = '" . $ProductPresent . "' AND
        `ProductMinimuim` = '" . $ProductMinimuim . "' AND
        `FotoUrl` = '" . $FotoUrl . "' AND
        `DBid` = '" . $DBid ."'
) LIMIT 1;
Community
  • 1
  • 1
Blue
  • 22,608
  • 7
  • 62
  • 92
  • thank you for the anwersing, I will the the OPP part en the Bind variables later. I always want a working query before i do the bind variables part. But it did not work the error is Error description: 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 ' `ProductBarcode` = '8717333541583', `ProductPakking` = 'zak', ' at line 6 – johndeer Jul 28 '16 at 23:48
  • 1
    Post the full query that's causing the issue. – Blue Jul 28 '16 at 23:52
  • I copy and paste your query into my code , but i still get the error – johndeer Jul 28 '16 at 23:59
  • I need to see the exact query that's being run – Blue Jul 28 '16 at 23:59
  • 1
    Just echo out $query instead of running it. Post back – Blue Jul 29 '16 at 00:02
  • If I echo the query i get this: INSERT INTO `card`(ProductName,ProductBarcode,ProductPakking,ProductPresent,ProductMinimuim,FotoUrl,DBid) SELECT * FROM ( SELECT 'brood','8717333541583','zak','-1937','11','img/img.svg','3') AS tmp WHERE NOT EXISTS ( SELECT name FROM `card` WHERE `ProductName` = 'brood', `ProductBarcode` = '8717333541583', `ProductPakking` = 'zak', `ProductPresent` = '-1937', `ProductMinimuim` = '11', `FotoUrl` = 'img/img.svg', `DBid` = '3' ) ) LIMIT 1 – johndeer Jul 29 '16 at 00:06
  • 1
    Edit your original post – Blue Jul 29 '16 at 00:07
  • all my tables in my DB are VARCHAR – johndeer Jul 29 '16 at 00:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118584/discussion-between-robin-de-cock-and-frankerz). – johndeer Jul 29 '16 at 00:10
  • 1
    I've updated my answer @RobindeCock – Blue Jul 29 '16 at 00:12
  • Thank you agian, I think where almost there. Now I get this: Error description: 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 ') LIMIT 1' at line 13 – johndeer Jul 29 '16 at 00:15
  • Fix this error on line 13, now this one Error description: Unknown column 'name' in 'field list' – johndeer Jul 29 '16 at 00:31
  • 1
    At this point, I've edited my answer to fix a mistaken `)` that was left out, and fixed an issue with your column name. – Blue Jul 29 '16 at 00:35
  • THANK YOU , If you were here i probably huged you or something, you made my day ! now the BIND VARIABLES and i'm done :D thank you !!!!!!!!!!!!! – johndeer Jul 29 '16 at 00:42
  • If my answer helped you, please be sure to mark it as answered (With the checkmark below the arrows next to the answer). – Blue Jul 29 '16 at 00:44
  • Sorry I almost forget that – johndeer Jul 29 '16 at 00:47