【发布时间】:2016-04-13 04:53:24
【问题描述】:
当我测试这个表单数据输入时,我没有遇到任何问题。当我尝试添加更复杂的文本时,在$post_content = $_POST['post_content']; 使用数字和非字母字符拾取的文本区域中,我遇到了这个错误。
查询失败:您的 SQL 语法有错误;查看与您的 MariaDB 服务器版本相对应的手册,了解在附近使用的正确语法
我的代码如下:
<?php
if (isset($_POST['create_post'])){
$post_title = $_POST['title'];
$post_author = $_POST['author'];
$post_category_id = $_POST['post_category_id'];
$post_status = $_POST['post_status'];
$post_image = $_FILES['image']['name'];
$post_image_temp = $_FILES['image']['tmp_name'];
$post_tag = $_POST['post_tag'];
$post_content = $_POST['post_content'];
$post_date = date('d-m-y');
$post_comment_count = 4;
move_uploaded_file($post_image_temp, "../images/$post_image");
$query = "INSERT INTO posts (post_category_id, post_title, post_author, post_date, post_image, post_content, post_tag, post_comment_count, post_status) ";
$query .= "VALUES ({$post_category_id},'{$post_title}','{$post_author}',now(),'{$post_image}','{$post_content}','{$post_tag}','{$post_comment_count}','{$post_status}' ) ";
$create_post_query = mysqli_query($connection, $query);
checkQuery($create_post_query);
}
?>
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="post_status">Post Title</label>
<input type="text" class="form-control" name="title">
</div>
<div class="form-group">
<label for="post_category">Post Category Id</label>
<input type="text" class="form-control" name="post_category_id" placeholder="Please enter a number">
</div>
<div class="form-group">
<label for="post_author">Post Author</label>
<input type="text" class="form-control" name="author">
</div>
<div class="form-group">
<label for="post_status">Post Status</label>
<input type="text" class="form-control" name="post_status">
</div>
<div class="form-group">
<label for="post_image">Post Image</label>
<input type="file" name="image">
</div>
<div class="form-group">
<label for="post_tag">Post Tags</label>
<input type="text" class="form-control" name="post_tag">
</div>
<div class="form-group">
<label for="post_tags">Post Content</label>
<textarea class="form-control" name="post_content" id="" cols="30" rows="10"></textarea>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="create_post" value="Publish Post">
</div>
</form>
我尝试将mysqli_real_escape_string 与$post_content = $_POST['post_content']; 结合使用,但没有成功,这让我认为我的查询语法已关闭。任何帮助将不胜感激。
【问题讨论】:
-
您应该使用准备好的和绑定的查询。像这样的查询更容易成功,您不必担心 sql 注入。
-
回显您的查询并检查它返回的内容!
-
您对SQL injection 敞开心扉。您正在遇到错误,因为您正在破坏自己的脚本。正如@Rasclatt 所说,您应该使用准备好的语句。
-
附近什么查询失败了?
-
为了调试查询的语法问题,请在尝试执行之前回显或打印 SQL 文本($query 的内容)。将 SQL 文本传送到另一个客户端并进行测试。此处显示的代码对于SQL 注入 来说是易受攻击的。使用带有绑定占位符的准备好的语句。不要将潜在的不安全值合并到 SQL 文本中。 https://www.owasp.org/index.php/SQL_Injection
标签: php mysqli mysql-error-1064