gpt4 book ai didi

php - 使用php将图像添加到mysql

转载 作者:行者123 更新时间:2023-11-29 07:23:30 24 4
gpt4 key购买 nike

您好,感谢您花时间阅读本文。我在将图像保存到数据库时遇到问题。评论的输入是一个字符串,评级的输入将进入,但图像不会保存到我的数据库中。尝试创建一个网站,从正在评价其发型的人那里拍摄照片。我想出的代码如下。

<form method="post" id = "bodyContainer" enctype="multipart/form-data">
<img src="" name="image" id="image"/>
<br/>
<input style="float:right;"type="file" name="dataFile" id="fileChooser" onchange="return ValidateFileUpload()" />

<SCRIPT type="text/javascript">
function ValidateFileUpload() {
var fuData = document.getElementById('fileChooser');
var FileUploadPath = fuData.value;

//To check if user upload any file
if (FileUploadPath == '') {
alert("Please upload an image");

} else {
var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();

//The file uploaded is an image
if (Extension == "gif" || Extension == "png" || Extension == "bmp" || Extension == "jpeg" || Extension == "jpg") {

// To Display
if (fuData.files && fuData.files[0]) {
var reader = new FileReader();

reader.onload = function(e) {
$('#image').attr('src', e.target.result);
}

reader.readAsDataURL(fuData.files[0]);
}

}

//The file upload is NOT an image
else {
alert("Photo only allows file types of GIF, PNG, JPG, JPEG and BMP. ");

}
}
}
</SCRIPT>

<textarea id="comment" name="comment" placeholder="Your comment..."></textarea><br/>
<input type="range" name="rating" min="1" max="5" id="rating"/><br/>
<input type="submit" name="submit" value="Submit" id="submitButton"/><br/>

我认为主要问题是由将代码添加到下面的数据库的 php 引起的。

<?php
if (isset($_POST['submit'])) {
if ($_POST['comment']) {

$comment = addslashes($_POST['comment']);
$rating = $_POST['rating'];

$image = addslashes($_FILES['image']['tmp_name']);
$name = addslashes($_FILES['image']['name']);
$image = file_get_contents($image);
$image = base64_encode($image) ;

$link = mysqli_connect("localhost", "root", "root","DJP");
$query = "INSERT INTO reviews (comment, image) VALUES ('$comment', '$image')";
$result = mysqli_query($link, $query);
if ($result) {
echo '<script type="text/javascript">alert("image uploaded");</script>';
}else{
echo '<script type="text/javascript">alert("image not uploaded");</script>';
}
}
}
?>

最佳答案

问题是由于您的 name 属性造成的。请看这里的声明,

<input style="float:right;"type="file" name="dataFile" id="fileChooser" onchange="return ValidateFileUpload()" />
^ see here

应该是

<input style="float:right;" type="file" name="image" id="fileChooser" onchange="return ValidateFileUpload()" />

关于php - 使用php将图像添加到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35284668/

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