无法将post和images值添加到数据库中

时间:2016-10-17 01:24:12

标签: php mysqli

我尝试使用

添加文字和图片

图像已成功添加到文件夹路径中。但是文本和图像的值没有添加到数据库中

<?php

    function insertpost(){
        global $connect;
        if(isset($_POST['sendpost']))
        {
            $title                 = mysqli_real_escape_string($connect,$_POST["title"]);
            $target_image          = "images/".basename($_FILES['post_image']['name']);
            $post_image            = $_FILES['post_image']['name'];

            $insert_post_and_image = "INSERT INTO table(title, image) VALUES ('$title','$post_image')";
            mysqli_query($connect, $insert_post_and_image);

                if(move_uploaded_file($_FILES['post_image']['tmp_name'], $target_image))
                {
                        echo "<h3>Posted to timeline!</h3>";
                }
        }   
    }
?>

html代码

<form action="" method="post" id="form" enctype="multipart/form-data">
                        <input type="text" name="title"/>
                        <input type="file" name="post_image"/>
                        <input type="submit" name="sendpost" value="POST"/> 
</form>

任何解决方案?谢谢

2 个答案:

答案 0 :(得分:0)

我认为您的查询中的table名称有误。只需将表替换为您的实际表名。例如,如果您已将表名定义为用户,那么您的查询必须是

$insert_post_and_image = "INSERT INTO user(title, image) VALUES ('$title','$post_image')";

您也可以使用

检查错误
if (mysqli_query($connect, $insert_post_and_image)) {
    echo "success";
} else {
    echo "Error: " . mysqli_error($connect);
}

答案 1 :(得分:0)

*校正 嗨,已经找到了答案。我不确定它是否正确。请指教。谢谢:)

<?php

    function insertpost(){
        global $connect;
        if(isset($_POST['sendpost']))
        {
            $title                 = mysqli_real_escape_string($connect,$_POST["title"]);
            $target_image          = "images/".basename($_FILES['post_image']['name']);
            $post_image            = $_FILES['post_image']['name'];

            $insert_post_and_image = "INSERT INTO table(title, image) VALUES ('$title','$post_image')";
            $result = mysqli_query($connect, $insert_post_and_image);

                if($result)     
                {
                        move_uploaded_file($_FILES['post_image']['tmp_name'], $target_image);
                        echo "<h3>Posted to timeline!</h3>";
                }
        }   
    }
?>