【问题标题】:Query will not insert into Database. (Correct Credentials)查询不会插入数据库。 (正确的凭证)
【发布时间】:2018-09-07 04:29:49
【问题描述】:

我遇到了一个奇怪的问题,即未添加将值插入数据库的查询。我通过终端使用 MySQL,我通过这些查询执行这些查询的文件在我的 AWS ubuntu 实例上。

这是位于我的 AWS 实例上的 upload.php 文件:

<?php

    if(isset($_POST["submit"])){
        $check = getimagesize($_FILES["image"]["tmp_name"]);
        if($check !== false){
            $image = $_FILES['image']['tmp_name'];
            $imgContent = addslashes(file_get_contents($image));

            $servername = "localhost";
            $username = "NOTREAL";
            $password = "NOTREAL";
            $dbname = "CS4830";

            //connect
            $db = new mysqli($servername, $username, $password, $dbname); 

            // Check connection
            if($db->connect_error){
                die("Connection failed: " . $db->connect_error);
            }
            else{
                echo "Successful";
            }

            $dataTime = date("Y-m-d H:i:s");    

            //Insert image content into database
            $insert = $db->query("INSERT into images (image, created) VALUES ('$imgContent', '$dataTime')");
            if($insert){
                echo "<br/>";
                echo "File uploaded successfully.";
            }else{
                echo "<br/>";
                echo "File upload failed, please try again. ";
            } 
        }else{
            echo "Please select an image file to upload.";
        }
    }

?>

当我上传图片并且 php 运行时,我最终得到“文件上传失败,请重试”。打印在我的屏幕上。 - 该表被命名为图像。 任何人都可以确定可能的问题吗?我想这可能与权限有关,但凭据是正确的,我无法想象缺少什么权限。除此之外,我现在迷路了。 谢谢。

(一个简单的演示站点) http://ec2-54-158-61-31.compute-1.amazonaws.com/images/

【问题讨论】:

  • 您好,您是否检查过您使用的凭据 ('$username'@'$servername') 是否具有正确的GRANTS 来处理您的请求?
  • print_r($imgContent) 打印此行并显示结果
  • @Avidos 我还没有检查他们被授予了什么,我该怎么做?我的 apache 服务器 (cgcnbf) 的用户名与 MySQL (root) 让我登录的用户名不同。会不会是这个问题?
  • @BilalAhmed 我打印了,它是一个带有符号、字符和问号的超长字符串。我会将上面的链接更新到我的服务器,以便您查看。
  • 如果您使用的那个没有正确的GRANT 到您要处理的&lt;database&gt;.&lt;table&gt;,那只会导致问题。尝试SHOW GRANTS FOR '&lt;your_user&gt;'@'&lt;target_host&gt;'; 例如SHOW GRANTS FOR 'cgcnbf'@'%';

标签: php mysql


【解决方案1】:

图片有多种存储方式

如果要将图像存储到数据库中,则需要 blob 数据类型(列数据类型必须为 BLOB) 那么你可以使用下面的代码

$imagename=$_FILES["myimage"]["name"]; 

//Get the content of the image and then add slashes to it 
$imagetmp=addslashes (file_get_contents($_FILES['myimage']['tmp_name']));

//Insert the image name and image content in image_table
$insert_image="INSERT INTO image_table VALUES('$imagetmp','$imagename')";

mysql_query($insert_image);

Code reference

如果要将图像添加到目录并将名称存储到数据库中,那么

$uploaddir = '/var/www/uploads/';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

    echo "<p>";

    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
      echo "File is valid, and was successfully uploaded.\n";
    } else {
       echo "Upload failed";
    }
//and then execute insert query with image name instead of image

code reference

【讨论】:

    【解决方案2】:

    您不能以这种方式存储图像内容。 所以“$imgContent = addlashes(file_get_contents($image));”中有一些问题

    如果要将图像数据保存到数据库中,请将图像内容转换为base64编码,然后尝试保存编码数据。

    【讨论】:

      【解决方案3】:
      $file_name = $_FILES['image']['name']; 
      $tmpfilename = $_FILES["image"]["tmp_name"]; 
      move_uploaded_file($tmpfilename,"images/".$file_name);  
      

      【讨论】:

      • 还要提一下插入查询的流程
      • 使用 $conn = mysqli_connect($server,$user,$psw,$db);
      • 欢迎来到stackoverflow!您的答案很好,但不完整且格式不正确。这将影响您的声誉
      猜你喜欢
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多