php形式的意外错误消息(SQL语法错误) [英] unexpected error message in php form (SQL syntax error)

查看:63
本文介绍了php形式的意外错误消息(SQL语法错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用数据库制作了一个简单的php cms表单,但是当我想用一些虚拟数据提交表单时,它不能正常工作!我不知道为什么会发生&我还添加了mysqli_error()来获取我所面临的错误类型,但是我只得到了这个:

I have made a simple php cms form with database but it does not work properly when I want to submit the form with some dummy data! I don't know why it happens & also I added the mysqli_error() to get the type of error that I'm facing with but I only got this:

您的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>

如果您对这个问题分享您的解决方案,这对我将非常有帮助...谢谢!

It would be very helpful to me if you share your solution for this problem... thanks!

推荐答案

您在 $ post_image 前缺少引号:

,$post_image'

应该是:

,'$post_image'

因此,完整的SQL语句变为:

So the complete SQL statement becomes then:

$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中进行分配:

Please note that you are doing assignments in this if:

if ($post_title=='' or $post_keywords='' or $post_content='' or $post_author=''){

您应该使用双==而不是=.

最后,您的代码容易受到 SQL注入的攻击.因此,请使用带有参数的准备好的语句.

Finally, your code is vulnerable to SQL injection. So please use prepared statements with parameters.

这篇关于php形式的意外错误消息(SQL语法错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆