Php 无法在数据库中存储文件详细信息
我正在尝试将上载的文件详细信息存储在我的数据库中。我已经编写了以下代码,但我无法理解为什么它不读取查询块。它不会生成任何MySQL错误消息或任何其他语法错误。请检查一下Php 无法在数据库中存储文件详细信息,php,html,mysql,database,syntax-error,Php,Html,Mysql,Database,Syntax Error,我正在尝试将上载的文件详细信息存储在我的数据库中。我已经编写了以下代码,但我无法理解为什么它不读取查询块。它不会生成任何MySQL错误消息或任何其他语法错误。请检查一下 <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <
<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注入攻击,因为您没有清理输入。您应该使用至少一种方法来确保不会发生这种情况
如果上述方法不起作用,请尝试检查连接参数是否正常以及MySQL是否正在运行。首先,不要使用MySQL,因为它会贬值。第二,echo“是否存储在:”回显任何内容?好的,下次我会处理它,是的‘echo存储在’显示路径;存储在:upload/paint.pngTryecho mysql_error()代码>显示mysql错误。我只是将这些行粘贴到我的代码中,并对旧的行进行注释。它不会生成任何错误消息,但也不会显示任何结果。更新答案。尝试echo$sql,复制输出并将其粘贴到phpmyadmin中,然后查看您也得到了什么。它只是在数据库中添加了记录:),并向我显示了消息“成功”,但仍在试图理解它以前不工作的原因。无论如何,非常感谢:)你能回答我昨天问的关于如何从MySQL数据库检索图像的另一个问题吗。
$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());
}