我的代码在这里有什么问题吗?

时间:2016-01-24 08:44:53

标签: php mysql

每当我想用我的表单插入数据时,除了没有向db插入任何内容的内容字段外,一切都很好!

这是我的完整代码:

    <?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'];
    $post_summary = $_POST['summary'];

    if ($post_title=='' || $post_keywords=='' || $post_content='' || $post_author=='' || $post_summary==''){
        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, post_summary)
    VALUES ('$post_title', '$post_date', '$post_author', '$post_image', '$post_keywords', '$post_content' , '$post_summary')";
        $insert_post = mysqli_query($con,$insert_query);
        if ($insert_post){
            echo '<h3 style="color:green">Post has been added successfully.</h3>';
        }elseif (!$insert_post){
            echo mysqli_error($con);
        }else{
            echo "Somthing goes wrong! Please contact with our support team...";
        }
    }
}
?>
<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"><h6>Insert Post Summary</h6></td>
            <td align="center"><textarea name="summary" cols="5" rows="5"></textarea></td></br>
        </tr>
        <tr>
            <td align="center"><input type="submit" name="submit" value="Submit"/></td>
        </tr>
    </table>
</form>

问题是$post_content变量没有在我的表中的post_content字段中插入任​​何内容,但是其他变量正常工作&amp;还会出现成功消息(第21行)

这是我的表结构:

  

post_id =&gt; int(11)

     

post_title =&gt; varchar(100)

     

post_date =&gt; 日期

     

post_author =&gt; varchar(100)

     

post_image =&gt; 图片

     

post_keywords =&gt; 文字

     

post_content =&gt; 文字

     

post_summary =&gt; 文字

2 个答案:

答案 0 :(得分:0)

你的错误$post_content=''如果条件应为$post_content=='',在这种情况下,你将var $ post_content设置为空字符串,因此在数据库表中它将为空。

答案 1 :(得分:0)

问题是您在以下行中指定了分配而不是比较:

if ($post_title=='' || $post_keywords=='' || $post_content='' || $post_author=='' || $post_summary==''){

$post_content=''更改为$post_content == ''