我一直在研究這一段時間,無法弄清楚我做錯了什麼。正在上傳文件,但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