php - 无法在数据库中存储文件详细信息

标签 php html mysql database syntax-error

我正在尝试将上传的文件详细信息存储在我的数据库中。我编写了以下代码,但我无法理解为什么它不读取查询 block 。它不会生成任何 MySQL 错误消息或任何其他语法错误。请检查一下。

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="file_upload_test2.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="file" name="file2" id="file"><br>
<input type="file" name="file3" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>



</body>
</html>


<?php

include 'connect.php';

$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));


if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
//&& ($_FILES["file"]["size"] < 200000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 200000) . " kB<br>";




        $image_name=        $_FILES["file"]["name"];  

        $path=              move_uploaded_file($_FILES["file"]["tmp_name"],
                                    "upload/" . rand().$_FILES["file"]["name"]);            

            echo "Stored in: " . "upload/" . $_FILES["file"]["name"];     

            if (mysql_query ("Insert into category_images (image_name,image_location) VALUES ('$image_name', '$path')"))
            {
                echo "successfull"; 
                } 
                else {
                    mysql_error();
                    } 
      }
    }

else
  {
  echo "Invalid file";
  }


?> 

最佳答案

尝试

    $sql = "INSERT INTO `category_images` (`image_name`,`image_location`) VALUES ('".$image_name."', '".$path."')";

    $result = mysql_query($sql);
    if ($result)
    {
          // Successful query execution
          echo "successfull"; 
    } 
    else {
          // Some error occured while executing query.
          // Show some useful information using echo/print.
          // Then stop execution after taking other necessary steps

          die(mysql_error());
    }

此外,您的数据库很容易受到 SQL 注入(inject)攻击,因为您没有清理输入。您应该至少使用 mysql_real_escape_string()方法来确保这种情况不会发生。

如果上述方法不起作用,请尝试检查您的连接参数是否正常以及 MySQL 是否正在运行。

关于php - 无法在数据库中存储文件详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17826797/

相关文章:

javascript - 如何设置 Yeoman/Grunt 来运行 PHP?

php - 404子域rewriteRule的错误处理

javascript - 在 div 中以与在 textarea 中相同的方式包装文本

mysql - 在 MySQL 中比较日期时将 31 用于 "last day of the month"是否安全

php - 我怎么知道为什么图片没有以 codeigniter 形式上传

mysql - 如何在 MySQL 中为每个类别选择 3 行?

php - 使用 get_instance() 时自动完成 Codeigniter

php - 如何使用 jQuery 在特定的点击项上显示从 php 检索到的数据?

android - 你如何在android中解析HTML?

javascript - extjs 中的链接按钮