2014-11-24 148 views
0

所以我寫了這個腳本,我一直未能上傳消息,請指出我做錯了什麼地方,請爲腳本舉個好例子。該圖像沒有上傳到數據庫,但它被移動到目錄'upload /'。 $ start & $ stop變量是日期。我在這一天呆了好幾天。這是代碼。無法上傳圖像到數據庫

include "../config/database.php"; 

$title = $_POST['title']; 
$id = $_POST['id']; 
$genre = $_POST['genre']; 
$start = $_POST['start']; 
$stop = $_POST['stop']; 
$description = $_POST['description']; 

$target_dir = "upload/"; 
$target_file = $target_dir . basename($_FILES["image"]["name"]); 

$uploadOK = 1; 

$imagetype = pathinfo($target_file, PATHINFO_EXTENSION); 

if (isset($_POST["submit"])) { 
    $check = getimagesize($_FILES["image"]["tmp_name"]); 
    if ($check !== false) { 
     echo "File is an image -" . $check["mime"] . "."; 
     $uploadOK = 1; 
    } else{ 
     echo "File isn't an image."; 
     $uploadOK = 0; 
    } 
} 

if (file_exists($target_file)) { 
    echo "Sorry, file is already exist."; 
    $uploadOK = 0; 
} 
if ($_FILES["image"]["size"] > 5000000) { 
    echo "Sorry, file is too large."; 
    $uploadOK = 0; 
} 

if ($imagetype != "jpg" && $imagetype != "jpeg" && $imagetype != "png" && $imagetype != "gif") { 
    echo "Sorry, only JPEG, JPG, PNG and GIF are allowed."; 
    $uploadOK = 0; 
} 

if ($uploadOK == 0) { 
    echo "Failed to upload."; 
} else { 
    if(move_uploaded_file($_FILES["image"]["tmp_name"] , $target_file)){ 

     $query = mysql_query("INSERT INTO anidata (id, title, image, genre, start, stop, description) VALUES ('$title', '$id', '$target_file', '$genre', '$start', '$stop', '$description') "); 
     $uploadOK = 1; 

     if ($query) { 
      header("Location: view.php"); 
     }else{ 
      echo "<p>Failed to upload</p>"; 
     } 
    } 
} 

我真的很感激任何幫助,謝謝:)

+0

[請不要在新代碼中使用'mysql_ *'函數](http://stackoverflow.com/q/12859942/1190388)。他們不再被維護,並[正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。改爲了解準備好的語句,然後使用[tag:PDO]或[tag:MySQLi]。 – hjpotter92 2014-11-24 01:33:48

回答

0

您的查詢的價值觀看起來他們是不是在正確的順序($ ID和$標題應該是倒過來)。改變這個;

$query = mysql_query("INSERT INTO anidata (id, title, image, genre, start, stop, description) VALUES ('$title', '$id', '$target_file', '$genre', '$start', '$stop', '$description') ");

此;

$query = mysql_query("INSERT INTO anidata (id, title, image, genre, start, stop, description) VALUES ('$id', '$title', '$target_file', '$genre', '$start', '$stop', '$description') ");

另外,還要確保你的變量類型都是按照你的表的字段(例如,你不嘗試保存一個字符串,其中一個整數應該是)。

+0

沒問題。如果解決了您的問題,請接受答案。 – tkounenis 2014-11-24 01:52:47