0

Hi all I need to do a check in the db if a value does not already exist. I should check in the field ' tracking_riparazione ' if the value taken from ': tracking_riparazione ' is not already entered.

$query = "
    INSERT INTO user_riparazioni (id_cliente,id_tipo_riparazione,category_id,brand_id,id_modello,id_stato,tracking_riparazione,imei,codice,difetto,accessori_consegnati,acconto,costo,note_esterne,note_interne,data_creazione,creato)
    VALUES (:id_cliente,:id_tipo_riparazione,:category_id,:brand_id,:id_modello,:id_stato,:tracking_riparazione,:imei,:codice,:difetto,:accessori_consegnati,:acconto,:costo,:note_esterne,:note_interne,:data_creazione,:creato)
    ";  

    $statement = $connect->prepare($query);
    $statement->execute(
        array(
            ':id_cliente'               =>  $_POST["id_cliente"],
            ':id_tipo_riparazione'      =>  $_POST["id_tipo_riparazione"],
            ':category_id'              =>  $_POST["category_id"],
            ':brand_id'                 =>  $_POST["brand_id"],
            ':id_modello'               =>  $_POST["id_modello"],
            ':id_stato'                 =>  $_POST["id_stato"],
            ':tracking_riparazione'     =>  $_POST["tracking_riparazione"],
            ':imei'                     =>  $_POST["imei"],
            ':codice'                   =>  $_POST["codice"],
            ':difetto'                  =>  $_POST["difetto"],
            ':accessori_consegnati'     =>  $_POST["accessori_consegnati"],
            ':acconto'                  =>  $_POST["acconto"],
            ':costo'                    =>  $_POST["costo"],
            ':note_esterne'             =>  $_POST["note_esterne"],
            ':note_interne'             =>  $_POST["note_interne"],
            ':creato'                   =>  $_SESSION["user_id"],
            ':data_creazione'           =>  date("d-m-Y")           

I tried adding this but to no avail, thanks in advance

         SELECT * FROM (SELECT 'tracking_riparazione') AS tmp
    WHERE NOT EXISTS (SELECT `tracking_riparazione` FROM `user_riparazioni` WHERE `tracking_riparazione` = ':tracking_riparazione') LIMIT 1
Tiaz993
  • 3
  • 2
  • 1
    `= ':tracking_riparazione'` shouldn't be quoted. – Funk Forty Niner Jan 10 '18 at 22:35
  • 1
    Maybe mark field as unique? – Demigod Jan 10 '18 at 22:38
  • You are making this more complicated then it has to be, just do `SELECT 1 FROM user_riparazioni WHERE tracking_riparazione = :tracking_riparazione` on `fetch` it will be `false` if it doesn't exist, or if it's on Inserts, make the field unique, and try/catch the exception, it's PDOException 23000 or something for duplicate keys, and it's way faster. That is what I do, it's like 10x faster to do Insert/Catch then Select/Insert – ArtisticPhoenix Jan 10 '18 at 22:47
  • excuse me, then how should I structure the whole? I did not understand much – Tiaz993 Jan 10 '18 at 22:53
  • Every field which should not have duplicate content should be marked as unique. You will receive an exception if you want to insert a row with already existing content in that field. – Demigod Jan 10 '18 at 22:57
  • but I need you to get out the message saying that already exists, if they just don't fit right imposed unique? – Tiaz993 Jan 11 '18 at 11:19

0 Answers0