1

Essentially I would like create a query that checks if the posted code already exists, if the code value already exists in the Table1 run the following update query:

$stmt = $conn->prepare('UPDATE Table1
    SET value = 2
    WHERE code = :codeNumber');
$stmt->execute([
    'codeNumber' => $_POST['code']
]);

If the code value does not exist run the following query:

$stmt = $conn->prepare( ' INSERT INTO `Table1` (`code`,`value`)
    VALUES (:code, :value) ' );
$stmt->execute([
    'code' => $_POST['code'],
    'value' => $_POST['value']
]);

How can something like this be done?

Dharman
  • 30,962
  • 25
  • 85
  • 135
John
  • 965
  • 8
  • 16
  • 2
    Sounds like you want to look up `INSERT INTO ... ON DUPLICATE KEY UPDATE` syntax. – Jonnix Aug 16 '19 at 17:30
  • Possible duplicate of: https://stackoverflow.com/questions/6107752/how-to-perform-an-upsert-so-that-i-can-use-both-new-and-old-values-in-update-par – Dharman Aug 16 '19 at 17:52
  • Using Duplicate Key method would assume that my code column contains a key? – John Aug 16 '19 at 19:57

0 Answers0