Загрузка файлов / изображений blob в Mysql

У меня был этот PHP-код, который принимает значения из html-формы (имя, файл, фотография, адрес, …..) и пытается вставить или обновить их в базе данных sql.

<?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name=""; // Database name $tbl_name=""; // Table name session_start(); $con=mysqli_connect("localhost","","",""); $id=$_REQUEST['id']; //Variable intilisation $name = ''; $remarcs = ''; $address = ''; $test_res = ''; $date = ''; $phone = ''; $new_path = ''; if (isset ($_POST['name'])) { $name = $_POST['name']; } if (isset ($_POST['remarcs'])) { $remarcs = $_POST['remarcs']; } if (isset ($_POST['test_res'])) { $test_res = $_POST['test_res']; } if (isset ($_POST['address'])) { $address = $_POST['address']; } if (isset ($_POST['date'])) { $date = $_POST['date']; } if (isset ($_POST['phone_num'])) { $phone = $_POST['phone_num']; } if(!empty($_FILES)){ //Check file is uploaded or not $check = getimagesize($_FILES["file"]["tmp_name"]); if($check !== false) { $path = "../uploads/".$_FILES['file']['tmp_name']; move_uploaded_file($file, $path); $new_path = $path; echo "Uploaded"; } if($check == false){ echo "Not uploaded"; } } if(move_uploaded_file($_FILES['file']['tmp_name'], $path)){ $sql=" update patients set values name = '$name', echo_photo = 'NULL', echo_file = '$new_path', remarcs = '$remarcs', test_res = '$test_res', date = '$date', address = '$address', phone_num = '$phone' WHERE id = ".$id; $result=mysqli_query($con,$sql) or die('Unable to execute query. '. mysqli_error($con)); header("location:update_done.php"); }else{ header("location:update_false.php"); } if($result){ echo $name."<p>\n</p>"; echo $remarcs."<p>\n</p>"; echo $test_res."<p>\n</p>"; echo $address."<p>\n</p>"; echo $phone."<p>\n</p>"; } mysqli_close($con); 

?>

Проблема в том, что я получаю следующие ошибки, когда я загружаю файл, показанный на изображении ниже: введите описание изображения здесь

И когда я не загружаю файл, у меня есть ошибка: выберите файл. Любая помощь ? Спасибо.

Это html-форма:

 <form action="update.php" id="Form2" method="POST" enctype="multipart/form-data" class="c"> <div align="center"> <?php echo "Updating information about Patient ".$row["name"]; ?> <table class="imagetable" border="1" cellspacing="3" align="center"> <th>Personal Informations</th> <th>Test Results</th> <tr><td>Name<br> <input type="text" class="large-fld" name="name" placeholder="Patient Name" value="<?php echo $row['name'];?>"/></td> <td>Remarcs:<br> <textarea type="text" cols="40" rows="5" class="large-fld" name="remarcs" placeholder="Remarcs"><?php echo $row['remarcs'];?></textarea></td> <tr><td>Address<br> <input type="text" class="large-fld" name="address" placeholder="Address" value="<?php echo $row['address'];?>"/> </td> <td>Test<br> <textarea type="text" cols="40" rows="5" class="large-fld" name="test_res" placeholder="Test Result"><?php echo $row['test_res'];?></textarea></td></tr> </td> </tr> <tr><td>Phone Number<br> <input type="text" class="large-fld" name="phone_num" placeholder="Phone Number" value="<?php echo $row['phone_num'];?>"/> </td> <th>Files</th> </tr> <td>Scanned Echo Photo<br> <input type="file" class="" name="echo_photo" id="echo_photo" placeholder="Add echo photo" value="<?php echo $row['echo_photo'];?>"/></td> <td>Echo Files:<br> <input type="file" name="file" id="file" value="<?php echo $row['echo_files'];?>"/><br></td> </tr></th></table> <div class="row" align="center"> <input type="submit" name="submit" id="btnUploadId" class="large-btn" value="Update" onClick="btnOnClickUpload()"> <input type="hidden" id="courseIdHidden" value="<?php echo $idd; ?>" /></td></tr> </table></div> </form> 

Попробуйте инициализировать переменную. Я добавил весь код. Вам не нужно получать $ _POST ['файл'].

 <?php $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="clinic"; // Database name $tbl_name="patients"; // Table name session_start(); $con=mysqli_connect("localhost","root","root","clinic"); $id=$_REQUEST['id']; $name = ''; $remarcs = ''; $address = ''; $test_res = ''; $date = ''; $phone = ''; $new_path = ''; if (isset ($_POST['name'])) { $name = $_POST['name']; } if (isset ($_POST['remarcs'])) { $remarcs = $_POST['remarcs']; } if (isset ($_POST['test_res'])) { $test_res = $_POST['test_res']; } if (isset ($_POST['address'])) { $address = $_POST['address']; } if (isset ($_POST['date'])) { $date = $_POST['date']; } if (isset ($_POST['phone_num'])) { $phone = $_POST['phone_num']; } if(isset($_FILES['file'])){ //Check file is uploaded or not $path = "../uploads/".$_FILES['file']['name']; if(move_uploaded_file($_FILES["file"]["tmp_name"], $path)){ $new_path = $path; $sql=" update patients set name = '$name', echo_photo = 'NULL', echo_file = '$new_path', remarcs = '$remarcs', test_res = '$test_res', date = '$date', address = '$address', phone_num = '$phone' WHERE id = ".$id; $result=mysqli_query($con,$sql) or die('Unable to execute query. '. mysqli_error($con)); if($result){ echo $name."<p>\n</p>"; echo $remarcs."<p>\n</p>"; echo $test_res."<p>\n</p>"; echo $address."<p>\n</p>"; echo $phone."<p>\n</p>"; } echo "Uploaded"; } else { echo "Not uploaded"; } } mysqli_close($con); ?> 

Это окончательный рабочий код:

 if (isset ($_POST['name'])) { $name = $_POST['name']; } if (isset ($_POST['remarcs'])) { $remarcs = $_POST['remarcs']; } if (isset ($_POST['test_res'])) { $test_res = $_POST['test_res']; } if (isset ($_POST['address'])) { $address = $_POST['address']; } if (isset ($_POST['date'])) { $date = $_POST['date']; } if (isset ($_POST['phone_num'])) { $phone = $_POST['phone_num']; } if(isset($_FILES['file'])){ //Check file is uploaded or not $path = "../uploads/".$_FILES['file']['name']; //$path2 = "../uploads/".$_FILES['echo_photo']['name']; if(move_uploaded_file($_FILES["file"]["tmp_name"], $path)){ $new_path = $path; $sql="UPDATE $tbl_name SET name = '$name', echo_files = '$new_path', remarcs = '$remarcs', test_res = '$test_res', date = '$date', address = '$address', phone_num = '$phone' WHERE id = '$id'"; $result=mysqli_query($con,$sql) or die('Unable to execute query. '. mysqli_error($con));