I need to submit ( insert into one table ) multiple text inputs with a file upload input via one php form but the whole operation failes and I got an error ( failed to add ) and after checking the error I found that this happens because of an empty input ( the upload file input ) because of the feilds validation, actually that happens ( the empty field ) because of the error ( undefined index ( the input file upload value ).
So I need help to get rid of this to complete sending the data.
and here is the upload.php code and the html form.
<form enctype="multipart/form-data" name="add_book" action="upload.php" method="post">
<input type="text" name="book_title" placeholder="Book Title .." required="required">
<input type="file" name="file_up" accept="image/*" required="required"/>
<input type="submit" name="add_book" value="Submit">
</form>
<?php
if (isset($_POST['add_book'])) {
$req_fields = array('book_title','file_up');
validate_fields($req_fields);
if(empty($errors)){
$book_title = (isset($_POST['book_title ']) ? $_POST['book_title '] : null);
$file_up = (isset($_POST['file_up']) ? $_POST['file_up'] : null);
$query = "SELECT `id` FROM `books` WHERE `book_title ` = '$book_title ' OR `file_up` = '$file_up' LIMIT 1";
$result = $db->query($query);
if ($result && $db->num_rows($result) > 0) {
//failed because the entered one of the two values is already exists in the db
echo "error already exists";
return ('page.php');
}
// upload file
if (!empty($_FILES['file_up'])) {
if(move_uploaded_file($_FILES["file_up"]["tmp_name"], "files/" . $_FILES["file_up"]["name"]))
$file_up = "files/" . $_FILES["file_up"]["name"];
echo "successfully added";
return ('page.php');
}
else
$file_up='';
echo "failed to add";
return ('page.php');
// insert data to db
$query = "INSERT INTO `books` (`book_title`, `file_up`) VALUES ('$book_title', '$file_up')";
if($db->query($query)) {
echo "successfully added";
return ('page.php');
} else {
echo "failed to add";
return ('page.php');
}
}
else {
echo $errors;
return ('page.php');
}
}
?>