上传图像到数据库失败

时间:2014-11-24 01:27:14

标签: php mysql image 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>";
        }
    }
}

我真的很感激任何帮助,谢谢你:)。

1 个答案:

答案 0 :(得分:0)

您的查询值看起来不正确($ id和$ title应该相反)。改变这个;

$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') ");

另外,请确保您的变量类型与表格的字段一致(例如,您不要尝试保存整数应该是的字符串)。