无法将图像发送到Mysql Server

时间:2016-06-24 02:22:09

标签: php jquery html mysql ajax

我的代码遇到问题。我正在尝试制作上传功能,以便将广告发送到数据库。它简单包含一个图像。这是我的PHP文件

query

如果是语法,请记住,我无法调试它。我的电脑上的服务器(xampp)不再有效,所以我只是将它上传到我的在线网络服务器并通过视线播放。它显示没有错误或调试信息。

这是我的“表单”,它是由ajax创建的,因此删除了表单标记。

<?php
    $servername = ""; //Taken out for stack overflow question
    $username = ""; //Taken out for stack overflow question
    $password = ""; //Taken out for stack overflow question  
    $dbname = ""; //Taken out for stack overflow question

    $conn = mysqli_connect($servername, $username, $password, $dbname);

    if (!$conn)
    {
        echo 'Could not connect';
    }
    else 
    {
        $cf = $_POST['cf']; //Image file 
        $category = $_POST['category']; //Category
        mysqli_query($conn, "INSERT INTO".$category."VALUES(".$cf.")"); //Query
    }

    mysqli_close($conn);
?>

最后是事件监听器

            <div class="modal fade chromeModal">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h2>Chrome form</h2>
                        </div>
                        <div class="modal-body">
                            <div class="imgPreview">

                            </div>
                        </div>
                        <div class="modal-footer">
                            <div role="form" class="form-inline alignButtons">
                                <div class="form-group">
                                    <label for="category"></label>
                                    <select class="form-control" id="category">
                                        <option id="cat1">Food</option>
                                        <option id="cat2">Software</option>
                                        <option id="cat3">Hardware</option>
                                        <option id="cat4">Home</option>
                                        <option id="cat5">Outdoor</option>
                                        <option id="cat6">Indoor activities</option>
                                        <option id="cat7">Services</option>
                                        <option id="cat8">Other</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label class="btn btn-primary btn-file">
                                        Upload <input type="file" class="btn btn-primary" id="upload" name="cf" style="display:none;">
                                    </label>
                                </div>
                                <div class="form-group">
                                    <button type="button" class="btn-default" id="submit">Submit</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

但我无法让它发挥作用。我究竟做错了什么?我该怎么做才能解决它?

1 个答案:

答案 0 :(得分:-1)

您的类别似乎缺少名称变量。添加一个名称以识别它,以便您的POST能够阅读此内容。

 <select class="form-control" name="category" id="category">
                                    <option id="cat1">Food</option>
                                    <option id="cat2">Software</option>
                                    <option id="cat3">Hardware</option>
                                    <option id="cat4">Home</option>
                                    <option id="cat5">Outdoor</option>
                                    <option id="cat6">Indoor activities</option>
                                    <option id="cat7">Services</option>
                                    <option id="cat8">Other</option>
                                </select>