2016-01-27 29 views
0

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

您必須執行您的查詢($ sql)。 – hherger

+0

你的'mysqli'查詢執行函數在哪裏? –

+0

是你的名字插入數據庫?如果它插入那麼查詢是正確的。嘗試在將圖像插入數據庫之後運行查詢,並使用時間戳更改圖像的名稱,這樣會很好,否則圖像的相同名稱可能會在某個級別引起混淆。 – nerdyDev

回答

1

插入查詢是錯誤的,您的查詢更改爲以下:

$sql = "INSERT INTO candidate_images (name,image,district_id) VALUES ('$name','$pic','$district_id')"; 

mysqli_query($connection, $sql); 

有關詳情,請參考:mysqli_query

我在這裏提供例如,如何插入到數據庫:

<?php 
    // Database connection establishment 
    $con=mysqli_connect("localhost","username","password","db_name"); 

    // Check connection 
    if (mysqli_connect_errno($con)) { 
    echo "MySQL database connection failed: " . mysqli_connect_error(); 
    } 

    $sql = "INSERT INTO candidate_images (name,image,district_id) VALUES ('$name','$pic','$district_id')"; 

    // Insert data into database 
    mysqli_query($con,$sql); 

?> 
+0

沒錯,$ pic上的尾部撇號丟失了,通過使用您的建議並添加mysqli_query並添加if(!mysql_query($ sql))來找到它echo echo('處理'。 '提交時發生數據庫錯誤。 \\ n如果此錯誤仍然存​​在,請' 'contact [email protected]。\\ n'。mysql_error()); 非常感謝! – iconMatrix

+0

@iconMatrix最受歡迎.. :)高興地幫助。 –

0
$sql = "INSERT INTO candidate_images (`name`,`image`, `district_id`) VALUES ('$name','$pic','$district_id')"; 

使用您的mysql_*mysqli_*PDO功能在這裏執行查詢數據庫。

0

其實你的插入查詢是錯誤的。你只需要糾正它。它會正常工作。 只需用我的下面的代碼替換你的代碼,別忘了執行保存在$ sql變量中的查詢。這也是必要的。如果我的工作不正常,請使用您自己的班級/模塊執行查詢。

<!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` (name,image,district_id) VALUES ('$name','$pic','$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"; 

mysql_query($sql); // you need to execute the $sql query here to insert the data into database 
} 

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.";} 
?> 
相關問題