【问题标题】:failed upload image to database上传图片到数据库失败
【发布时间】:2014-11-24 01:27:14
【问题描述】:

所以我写了这个脚本,但我一直无法上传消息,请指出我做错的地方,并为脚本提供一个很好的例子。图片未上传到数据库,但已移至目录“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>";
        }
    }
}

非常感谢任何帮助,谢谢:)

【问题讨论】:

标签: php mysql image upload


【解决方案1】:

您的查询值看起来顺序不正确($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') ");

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

【讨论】:

  • 没问题。如果它解决了您的问题,请接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-25
  • 2020-12-07
  • 2014-03-29
  • 2013-12-25
相关资源
最近更新 更多