Is there any way to check if there will be an error on file uploading? Like putting a variable to know if there’s an error on every else statement so that before it will prevent running the query. Because what’s happening is even there’s an error on moving/copying/deleting a file, it still runs the query.
$dir = './uploads/images/'; if ($_FILES['thumbnail']['size'] == 0 && $_FILES['thumbnail']['error'] > 0){ $new_path = $dir; $new_file = $new_path . $exist_tn; $move = rename($cur_file,$new_file); if ($move) { $thumbnail = $exist_tn; } else { echo "Error Moving File."; } }else{ $thumbnail = $thumb; $deletefile = unlink($cur_file); if($deletefile){ $targetPath = $dir.$thumbnail; $sourcePath = $_FILES['thumbnail']['tmp_name']; $movefile = move_uploaded_file($sourcePath,$targetPath); if ($movefile) { echo "File upload successfully."; }else{ echo "Upload File Failed."; } }else{ echo "Delete File failed."; } } $query = ' UPDATE `mytbl` SET `title` = "'.$title.'", `thumbnail` ="'.$thumbnail.'", `date` = "'.$date.'", `part` = "'.$part.'", `tags`= "'.$tags.'" WHERE `id` = "'.$id.'" '; $update = mysqli_query(db(),$query); //Check if there's an error first then run query if (checkCondition) { if($update === TRUE){ echo "sql_update"; }else{ echo "sql_error"; } } else { echo "Error Found"; }
Advertisement
Answer
Use exit after echo if you don’t want the query to run e.g $dir = ‘./uploads/images/’;
if ($_FILES['thumbnail']['size'] == 0 && $_FILES['thumbnail']['error'] > 0){ $new_path = $dir; $new_file = $new_path . $exist_tn; $move = rename($cur_file,$new_file); if ($move) { $thumbnail = $exist_tn; } else { echo "Error Moving File."; exit(); } }else{ $thumbnail = $thumb; $deletefile = unlink($cur_file); if($deletefile){ $targetPath = $dir.$thumbnail; $sourcePath = $_FILES['thumbnail']['tmp_name']; $movefile = move_uploaded_file($sourcePath,$targetPath); if ($movefile) { echo "File upload successfully."; }else{ echo "Upload File Failed."; exit(); } }else{ echo "Delete File failed."; exit(); } } $query = ' UPDATE `mytbl` SET `title` = "'.$title.'", `thumbnail` ="'.$thumbnail.'", `date` = "'.$date.'", `part` = "'.$part.'", `tags`= "'.$tags.'" WHERE `id` = "'.$id.'" '; $update = mysqli_query(db(),$query); //Check if there's an error first then run query if (checkCondition) { if($update === TRUE){ echo "sql_update"; }else{ echo "sql_error"; } } else { echo "Error Found"; }