2014-11-24 144 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