将图像路径插入数据库

Inserting image path, into database

我一直在为一个 uni 项目制作一个网站,我的代码被指出容易受到 SQL 注入的攻击,因为我没有使用准备好的语句。我正在纠正这个问题,我已经 运行 撞墙了,上传了图片。当我只使用标准 MYSQLi 查询时,我理解它分配给变量的方式,现在当我使用占位符和绑定参数时,我有点迷路了。最初我无法将图像放入目录,尽管我现在已经通过阅读论坛解决了这个问题。现在我的图像路径没有存储在数据库中,我已经尝试了很多方法来让它工作并在论坛上广泛阅读。我无法在 MYSQLi 中使用 Prepare 语句获取图像上传的示例。希望有人能给我指出正确的方向,这样我就能更好地理解。

if(isset($_POST['add_product'])) {

    $destination = "C:/Ampps/www/Adaptive_Dev/images/".$_FILES['image']['name'];

    move_uploaded_file($_FILES['image']['tmp_name'], $destination);

    print_r($_FILES);

    $stmt = $connection->prepare("INSERT INTO products
                                (productBrand,
                                productModel,
                                productModelNo,
                                productPrice,
                                productDesc,
                                productSpec,
                                productInTheBox,
                                productGuarantee,
                                productImage)
                                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("sssdsssis",
        $_POST['productBrand'],
        $_POST['productModel'],
        $_POST['productModelNo'],
        $_POST['productPrice'],
        $_POST['productDesc'],
        $_POST['productSpec'],
        $_POST['productInTheBox'],
        $_POST['productGuarantee'],
        $_FILES['ProductImage'] );
    $stmt->execute();

我暂时将列设置为接受空值,以便将数据传递到数据库。我试图在图像的占位符位置放置一个变量,这引发了各种错误。我只是不知道如何将它传递给绑定参数。

Array ( [image] => Array ( [name] => b&oM5Banner.jpg [type] => image/jpeg [tmp_name] => C:\Ampps\tmp\phpB39.tmp [error] => 0 [size] => 269481 ) )

我已经把我的 print_r 结果放在这里了,我真的不知道我还能提供什么其他信息,我显然做错了什么,我不知道是什么。提前谢谢。

你的错误很明显,与准备好的语句无关。

你这样说

I've temporarily set the column to accept nulls,

你为什么要这样做,因为它告诉你哪里出了问题。您提供的路径为空。聆听收到的警告和错误通常是个好主意,因为它们会告诉您去哪里查看。

此外,如果您打开了错误报告,那么您应该已经收到有关使用未定义索引的警告。你可以像这样打开它:

<?php
    error_reporting(-1);
    ini_set('display_errors', 1);

但回到手头的问题,让我们看看您为图像路径输入的内容:

   $_FILES['ProductImage']

在最好的情况下,这是一个数组,而不是路径 ( string )。 $_FILES数组是PHP中的超全局,它有特殊用途。也就是说,该数组包含有关通过 HTTP 上传的文件的信息。您应该知道这一点,因为您使用它将文件从 "temp" 文件夹移动到永久位置。

考虑到所有这些,解决方案确实非常简单。只需使用 $destination 变量,这是您放置文件的路径。您可以在此处的代码中看到它:

  move_uploaded_file($_FILES['image']['tmp_name'], $destination);

为了详细说明,您的 $_FILES 超级全局数组的内容示例如下所示:

 $_FILES = [
      'image' => [
            'name' => 'b&oM5Banner.jpg',
            'type' => 'image/jpeg',
            'tmp_name' => 'C:\Ampps\tmp\phpB39.tmp',
            'error' => 0, 
            'size' => 269481
       ],
 ];
  //I know this because you output it with print_r

因此,当您尝试使用 $_FILES['ProductImage'] 时,它不起作用有什么奇怪的吗?

希望这有助于解释发生了什么以及如何解决它。

干杯。

$_FILES['ProductImage'] 似乎不存在,但您确实使用路径创建了一个名为 $destination 的变量,所以我假设您应该使用它。

if(isset($_POST['add_product'])) {

    $destination = "C:/Ampps/www/Adaptive_Dev/images/".$_FILES['image']['name'];

    move_uploaded_file($_FILES['image']['tmp_name'], $destination);

    print_r($_FILES);

    $stmt = $connection->prepare("INSERT INTO products
                                (productBrand,
                                productModel,
                                productModelNo,
                                productPrice,
                                productDesc,
                                productSpec,
                                productInTheBox,
                                productGuarantee,
                                productImage)
                                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("sssdsssis",
        $_POST['productBrand'],
        $_POST['productModel'],
        $_POST['productModelNo'],
        $_POST['productPrice'],
        $_POST['productDesc'],
        $_POST['productSpec'],
        $_POST['productInTheBox'],
        $_POST['productGuarantee'],
        //$_FILES['ProductImage'] );
        $destination );
    $stmt->execute();

In future while testing add these lines to the top of your script and errors will appear on the browser, even if you are developing with Error dispays turned off, like a hosting site.

ini_set('display_errors', 1); 
ini_set('log_errors',1); 
error_reporting(E_ALL); 
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);