Я уже давно работаю над этим и не могу понять, что я делаю неправильно.Файл загружается, но INSERT не добавляет места в базу данных
Этот сценарий кажется достаточно простым и добавляет файл на сервер, но он не вставляет местоположение в базу данных. Где я потерпел неудачу с частью INSERT кода?
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<form method="post" action="photo-upload2.php?district_id=<?php echo $_REQUEST['district_id']?>" enctype="multipart/form-data">
<p>
Please Enter the Candidate's Name.
</p>
<p>
Candidate's Name:
</p>
<input type="text" name="nameMember"/>
<p>
Please Upload a Photo of the Candidate in gif or jpeg format. The file name should be named after the Candidate's name.<br>
If the same file name is uploaded twice it will be overwritten! Maxium size of File is 35kb. </p>
<p>
Photo:
</p>
<input type="hidden" name="size" value="350000">
<input type="file" name="photo">
<br/>
<br/>
<input TYPE="submit" name="upload" title="Add data to the Database" value="Add Pic"/>
</form>
</body>
</html>
<?php
// This is the directory where images will be saved
$target = "../images/candidates/";
$target = $target . basename($_FILES['photo']['name']);
// This gets all the other information from the form
$name=$_POST['nameMember'];
$district_id=$_REQUEST['district_num'];
$pic=($_FILES['photo']['name']);
// Connects to your Database
include('db.php');
//Writes the information to the database
$sql = "INSERT INTO candidate_images SET
name='$name',
image='$pic,
district_id='$district_id'";
// Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
// Tells you if its all ok
echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else { // if name is blank
if ($name=='') { echo "Please enter Candidate's name.";
// Gives and error if it's not ok
} else echo "Sorry, there was a problem uploading your file $name.";}
?>
Вы должны выполнить запрос ($ sql). – hherger
где ваша функция выполнения запросов mysqli? –
Ваше имя вставляется в базу данных? если его вставка затем выполняется правильно. попробуйте запустить запрос после вставки изображения в базу данных и изменить имя изображения с помощью метки времени, это будет хорошо, иначе одно и то же имя изображения может запутаться на некотором уровне. – nerdyDev