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

标签 php html mysql

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

<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/

相关文章:

mysql - 从 shell 打印 mysql ROW_COUNT() 结果

php - MYSQL/PHP 如何从日期减去 7 天?

php - LexikJWTAuthenticationBundle | LexikJWTAuthenticationBundle | LexikJWTAuthenticationBundle | LexikJWTAuthenticationBundle | LexikJWTAuthenticationBundle | JwtToken 正在工作,但身份验证显示 "Full authentication is required to access this resource."

javascript宽度/高度不起作用和颜色

javascript - 有没有办法修复一个没有 float 的div?

MySQL 创建并使用带有变量的新数据库

PHP/MySQL - 使用 session 数据更新表数据

php - 如何在 php 中为选定的多个电子邮件地址列表一次发送电子邮件

javascript - OnClick 函数不适用于 HTML 按钮

MySQL-计算平均值的百分比