php形式的意外错误消息(SQL语法错误)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php形式的意外错误消息(SQL语法错误),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2565字,纯文字阅读大概需要4分钟。
内容图文
我已经用数据库制作了一个简单的php cms表单,但是当我想用一些虚拟数据提交表单时,它不能正常工作!我不知道为什么会发生&我还添加了mysqli_error()来获取我所面临的错误类型,但是我只有这样:
您的SQL语法有误;检查与您的MySQL服务器版本相对应的手册以在第2行的“,”,”)’附近使用正确的语法
<?php
if (isset($_POST['submit'])){
$post_title = $_POST['title'];
$post_date = date('d-m-y');
$post_author = $_POST['author'];
$post_keywords = $_POST['keywords'];
$post_content = $_POST['content'];
$post_image = $_FILES['image']['name'];
$image_tmp = $_FILES['image']['tmp_name'];
if ($post_title=='' or $post_keywords='' or $post_content='' or $post_author=''){
echo '<script>alert("Some fields are missing")</script>';
}else{
move_uploaded_file($image_tmp,"post_images/$post_image");
$insert_query = "INSERT INTO posts
(post_title,post_date,post_author,post_image,post_keywords,post_content) VALUES ('$post_title','$post_date','$post_author',$post_image','$post_keywords','$post_content')";
$insert_post = mysqli_query($con,$insert_query);
if ($insert_post){
echo '<h3 style="color:green">Post has been added successfully.</h3>';
}else{
echo mysqli_error($con);
}
}
}
?>
<form method="POST" action="" enctype="multipart/form-data">
<table width="600" align="center" border="10">
<tr>
<td align="center"><h6>Insert Post Title</h6></td>
<td align="center"><input type="text" name="title"/></td></br>
</tr>
<tr>
<td align="center"><h6>Insert Post Author</h6></td>
<td align="center"><input type="text" name="author"/></td></br>
</tr>
<tr>
<td align="center"><h6>Insert Post Keywords</h6></td>
<td align="center"><input type="text" name="keywords"/></td></br>
</tr>
<tr>
<td align="center"><h6>Insert Post Image</h6></td>
<td align="center"><input type="file" name="image"/></td></br>
</tr>
<tr>
<td align="center"><h6>Insert Post Content</h6></td>
<td align="center"><textarea name="content" cols="10" rows="10"></textarea></td></br>
</tr>
<tr>
<td align="center"><input type="submit" name="submit" value="Submit"/></td>
</tr>
</table>
</form>
如果您分享此问题的解决方案,这对我非常有帮助…谢谢!
解决方法:
您在$post_image之前缺少引号:
,$post_image'
应该:
,'$post_image'
因此,完整的SQL语句变为:
$insert_query = "INSERT INTO posts
(post_title, post_date, post_author, post_image, post_keywords, post_content)
VALUES ('$post_title', '$post_date', '$post_author', '$post_image',
'$post_keywords', '$post_content')";
请注意,如果您在以下情况下正在做作业:
if ($post_title=='' or $post_keywords='' or $post_content='' or $post_author=''){
您应该使用double ==而不是=.
最后,您的代码容易受到SQL injection的攻击.因此,请使用带有参数的prepared statements.
内容总结
以上是互联网集市为您收集整理的php形式的意外错误消息(SQL语法错误)全部内容,希望文章能够帮你解决php形式的意外错误消息(SQL语法错误)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。