0

I have this PHP Code with Query to mysql database:

$query2 = "IF( EXISTS (SELECT * FROM shipcargo WHERE shipid='$shipid' AND item='$item' AND price='$price')) 
    BEGIN
    UPDATE shipcargo SET amount = amount+'$amount' WHERE shipid='$shipid' AND item='$item' AND price='$price'
    END
    ELSE
    BEGIN
    INSERT INTO shipcargo (shipid, item, amount, price) VALUES('$shipid', '$item', '$amount', '$price')
    END";
    mysql_query($query2) or die(mysql_error());

The Error Returned is:

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 'IF( EXISTS (SELECT * FROM shipcargo WHERE shipid='11' AND item='WheatBastard' AN' at line 1

1
  • PHP does not trigger SQL errors. You have to check what exactly $query2 contains and see if it's valid SQL code. Commented Mar 5, 2012 at 11:05

2 Answers 2

1

syntax is wrong! Take a look at http://dev.mysql.com/doc/refman/5.0/en/exists-and-not-exists-subqueries.html

Sign up to request clarification or add additional context in comments.

Comments

0

I believe it's

IF EXISTS (SELECT * FROM

Rather than

IF (EXISTS ( SELECT

You don't need a bracket with the IF statement

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.