简体   繁体   中英

how to prevent insertion of the existed data in SQL

I'm been working for a codes to avoid data insertion that's already existed, but I left it undone because I have to work another important features of my system, now, when I want to code it back using the suggestions and answers from this site, I found it so stressful, because my SQL statement is too crowded, resulting fatal error, now I have to ask what should be added to my SQL line when I want to avoid duplication of the data named invoice. here's how I constructed my codes:

if($_POST["controller"] == "add") {
    // validations here
    if(empty($_POST["main_date"])) {
        echo 'error'; die();
    }else{
        // upload file
        $uniquesavename = time().uniqid(rand());
        $extension = pathinfo($_FILES['fileToUpload']['name'], PATHINFO_EXTENSION);
        $target_dir = "../files/laboratory/receipt/";
        $uploadOk = 1;

        $target_file = $target_dir . $uniquesavename . '.' . $extension;

        // check filesize
        if ($_FILES["fileToUpload"]["size"] > 1000000) {
            $uploadOk = 0;
        }

        if ($uploadOk == 0) {
            echo 'error in upload attachment 1'; die();
        // if everything is ok, try to upload file
        } else {
            if (!move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                // move file failed
                echo 'error in upload attachment 2'; die();
            }
        }
    }

    $result = $_POST["employee"];
    $result_explode=explode('|', $result);
    $result = query("INSERT INTO tbl_maintenance 
        (employee_id,
        carid,
        account_id,
        main_date,
        invoice,
        up_date,
        previous,
        present,
        particular,
        code,
        amount,
        attachment,
        dateAdded) 
        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", 
        $result_explode[1],  
        $_POST["platenumber"], 
        $_POST["account_id"], 
        $_POST["main_date"], 
        $_POST["invoice"], 
        $_POST["up_date"], 
        $_POST["previous"], 
        $_POST["present"], 
        $_POST["particular"], 
        $_POST["code"], 
        $_POST["amount"], 
        $target_file,
        date("Y-m-d"));
    if($result === false) {
        echo 'mali imong code'; die();
    }
    else {
        $row = query("SELECT LAST_INSERT_ID() AS mId");
        $id = $row[0]["mId"];
        redirect("maintenance.php?profile=" . $id);
    }
}

I would update the table and add an UNIQUE constraint to it :

ALTER TABLE tbl_maintenance ADD CONSTRAINT invoice_unique UNIQUE (invoice);

When you'll try to insert a duplicated data, MySQL will return a unique constraint violation error. You'll have to check it manually with mysqli_error() or PDO::errorInfo() or whatever you are using to query your DB. (Hint : the error code is 1062 ).


If you prefer to update the duplicated row instead of doing nothing, you can use ON DUPLICATE KEY UPDATE such as :

INSERT INTO tbl_maintenance
(
    yourCols
)
VALUES
(
    yourValues
)
ON DUPLICATE KEY UPDATE
    col1 = val1,
    col2 = val2,
    ...

You don't necessarily need a lot of PHP for that. The only thing you need is:

try {
    $result = query($yourLongQuery);
} catch (Exception $e) {
    if(mysqli_errno() === '1062') {
        echo "Duplicate data!";
    } else {
        echo "Something went wrong!";
    }        
}

To make this work, you have to define UNIQUES on your table. By default, the primary will always be unique, but you can add more UNIQUE with:

ALTER TABLE tableName ADD UNIQUE INDEX indexName (columnName);

If you try to insert a duplicate, it will be errorcode 1062. You can catch that case and react accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM