0

I'm trying to insert records into my DB, if a duplicate is found it should only update one field.

This is my code so far

$strSQL = "INSERT INTO ppage_image_uploads(Cons_ID, Img_SRC, Program, TeamRaiser_ID) 
    VALUES
    ('" . $_POST["cons_id"] . "',
    '" . $_POST["img_src"] . "',
    '" . $_POST["program"] . "',
    '" . $_POST["tr_id"] . "')
    ON DUPLICATE KEY UPDATE
        Img_SRC = VALUES '" . $_POST["img_src"] . "'
    )";

Took this loosely from another question: MySQL ON DUPLICATE KEY UPDATE for multiple rows insert in single query

I'm ending up with an error message from my page that handles this script saying

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''theimageurl.jpg' )' at line 8

Does anything look wrong in that statement that I'm missing?

1 Answers1

0

There is an unmatched closing parenthesis at the end of your sql query.

The VALUES in the ON DUPLICATE KEY clause can be used to refer to the value that would have been inserted if no duplicate key conflict had happened, as explained in the documentation.

$strSQL = "INSERT INTO ppage_image_uploads(Cons_ID, Img_SRC, Program, TeamRaiser_ID) 
    VALUES
    ('" . $_POST["cons_id"] . "',
    '" . $_POST["img_src"] . "',
    '" . $_POST["program"] . "',
    '" . $_POST["tr_id"] . "')
ON DUPLICATE KEY UPDATE
    Img_SRC = VALUES(Img_SRC)";
GMB
  • 216,147
  • 25
  • 84
  • 135