php - 给定特定参数时,如何修复未运行的 SQL 查询?
问题描述
我正在创建一个论坛,允许用户通过 HTML 表单上传图像,然后使用 PHP 将其保存到特定文件夹。然后我计划使用 SQL 查询向表中添加一行并包含所选图像的文件路径,以便可以在另一个页面上显示它。
我对 PHP 和 mySQL 还很陌生,因此我一直在使用 Google 来获得很多答案。但是,我无法找到遇到此问题的其他人,并且在我的论坛中没有找到遇到类似问题的任何其他 SQL 查询。另外,我的代码没有收到任何错误。
<?php
include("../../../dbconn.php");
$cid = 1;
$scid = 1;
$topic_title = $_POST['topic-title'];
$content = $_POST['topic-content'];
$target_dir = "Saved Images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.</br>";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
mysqli_query($con, "INSERT INTO Artists_Looking_For_Writers (`category_id`, `subcategory_id`, `title`, `content`, `date_posted`, `imgName1`) VALUES ('".$cid."', '".$scid."', '".$topic_title."', '".$content."', NOW(), '".$_FILES['fileToUpload']['name']."');");
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
这应该在我的表中创建一个包含类别 id、子类别 id、标题、内容、发布日期和我的图像名称的行。相反,不会向表中添加任何内容。仅当我imgName1
在 SQL 查询中包含该列时才会发生这种情况。如果我删除它和获取上传文件名称的命令,那么它会完美运行。
解决方案
我解决了这个问题。问题在于我使用了单引号和双引号,然后我将查询移到了单独的 if 语句中,从而解决了问题。
推荐阅读
- powershell - 运行两个python文件的powershell脚本循环
- java - 如何在 Spring 的 WebClient 上验证 bean?
- python - 将具有可为空整数数据类型的 pandas DataFrame 保存到 HDF 文件(格式='table')
- elasticsearch - 如何提升与最佳结果相似的替代方案
- mysql - 如何在 MySQL 中为 SET 类型设置超过 1 个默认值?
- blazor - Mat Blazor mat-expansion-panel 移除高程/边框
- ruby - 对使用 reduce 及其实际尝试做的事情感到困惑
- java - 再次点击按钮以确认操作
- c# - 如何向 API 提交包含 536000 个字符的字符串
- python - 使用 tkinter 包几何管理器的帧之间的空间