0

I am having trouble with my code. I am trying to make a upload function to send ads to a database. It simple contains an image. Here is my PHP file

<?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);
?>

If it is syntax, keep in mind, I have no way to debug it. My server on my computer (xampp) no longer works, so I am just uploading it to my online web server and playing it by sight. It shows no errors or debug info.

Here is my "form" it is made by ajax so form tags are removed.

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

And lastly the event listeners

chromeOnly.addEventListener("click", function() {
    Notification.requestPermission(function(request) {
        console.log(request);
    });
    $(".chromeModal").modal('show');
    var chromeOnlyData = $('#upload').val();
    $.ajax({
        url: 'http://4lifeidiomas.com/smartlead/app/php/sendChrome.php',
        type: 'POST',
        data: chromeOnlyData,
        success: function(data)
        {
            var chromeSent = new Notification('Smart Lead Advertisment', {
                icon: 'http://4lifeidiomas.com/smartlead/app/logo.png',
                body: 'Successfully sent to Google Chrome extension!'
            });
        },
        error: function()
        {
            var chromeSentError = new Notification('Smart Lead Advertisment', {
                icon: 'http://4lifeidiomas.com/smartlead/app/logo.png',
                body: 'Error trying to send data to Google Chrome'
            });
        }
    });
}, false);

But I cannot get it to work. What am I doing wrong? What can I do to fix it?

  • Hi, you should be able to debug it with `$cf = $_POST['cf']; //Image file $category = $_POST['category']; //Category mysqli_query($conn, "INSERT INTO".$category."VALUES(".$cf.")"); //Query` change each value to a static value to check if your form is POST-ing correctly. – Ponilz Jun 24 '16 at 02:34

1 Answers1

-1

It seems like your category is missing a name variable. Add a name to identify it so that your POST is able to read this.

 <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>
Ponilz
  • 109
  • 1
  • 13