gpt4 book ai didi

php - 图像没有出现在 MYSQL 数据库中

转载 作者:搜寻专家 更新时间:2023-10-30 23:32:31 24 4
gpt4 key购买 nike

我的问题是,如果我尝试通过我的网站将图片发布到名为照片的数据库中,它们不会出现在那里。图片确实出现在我创建的 upload/img 文件夹中。我的数据库照片表称为 images with 2 rows。 id int(11) 和图像 MEDIUMBLOB。知道为什么我的图像没有出现在我的 MYSQL 数据库中吗?

    <?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<title>Image Upload</title>
</head>
<body>
<?php if(isset($_SESSION['err'])){ ?>
<h2><?php echo $_SESSION['err']; ?></h2>
<?php session_unset(); } ?>
<form method="post" action="upload.php" enctype="multipart/form-data" >
<input type="file" name="image" />
<input type="submit" value="Submit" name="save">
</form>
</body>
</html>

我的上传.php

<?php

require_once('config.php');
session_start();
if(isset($_POST['save']))
{
$target_dir = "upload/img/";
$filename = explode('.',$_FILES['image']['name']);
$ext = $filename[1];
$imgname = time().'.'.$ext;
$target_file = $target_dir . $imgname ;
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
$path=$imgname;
$conn->query("INSERT INTO images (id, image)VALUES ('$path')");
$_SESSION["Success"]='Image Is Upload Success...';
header("Location:view.php"); /* Redirect browser */
exit();
} else {
$_SESSION["err"]=$text;
header("Location:index.php"); /* Redirect browser */
exit();
}
}
}

?>

这是我的“图像”表的图片。

enter image description here

最佳答案

必须是

if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
$path=$imgname;
$conn->query("INSERT INTO images (image) VALUES ('$path')");
$_SESSION["Success"]='Image Is Upload Success...';
header("Location:view.php"); /* Redirect browser */
exit();
} else {
$_SESSION["err"]=$text;
header("Location:index.php"); /* Redirect browser */
exit();
}

因为你告诉 Mysql 插入 id 但你没有提供它的值

关于php - 图像没有出现在 MYSQL 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47461008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com