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);
}
}