0

Where do i put my PHP SQL query, to insert image information to my database? I tried just before the echo "success"; however it had no effect.

<!-- Upload Button-->
<div id="upload" >Upload File</div><span id="status" ></span>
<!--List Files-->
<ul id="files" ></ul>

PHP handling

<?php
$uploaddir = './uploads/';
$file = $uploaddir . basename($_FILES['uploadfile']['name']); 

if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
echo "success";
} else {
echo "error";
}
?>

Javascript section

$(function () {
    var btnUpload = $('#upload');
    var status = $('#status');
    new AjaxUpload(btnUpload, {
        action: 'upload-file.php',
        //Name of the file input box
        name: 'uploadfile',
        onSubmit: function (file, ext) {
            if (!(ext && /^(jpg|png|jpeg|gif)$/.test(ext))) {
                // check for valid file extension
                status.text('Only JPG, PNG or GIF files are allowed');
                return false;
            }
            status.text('Uploading...');
        },
        onComplete: function (file, response) {
            //On completion clear the status
            status.text('');
            //Add uploaded file to list
            if (response === "success") {
                $('<li></li>').appendTo('#files').html('<img src="./uploads/' + file + '" alt="" /><br />' + file).addClass('success');
            } else {
                $('<li></li>').appendTo('#files').text(file).addClass('error');
            }
        }
    });
});

1 Answer 1

1

The query should be right before success echo, so you have to verify what is returning to you the move_uploaded_file method.

Verify if ./upload/ is the right path and if you have read/write access (0777) on this directory.

Also make sure that you're connected to database:

<?php 

    $conn = mysql_connect("HOST","USER","PASSWORD");
    $db = mysql_select_db("DB_NAME");

    $uploaddir = './uploads/'; 
    $file = $uploaddir . basename($_FILES['uploadfile']['name']);  

    if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { 
        mysql_query("INSERT INTO photos VALUES ('your_values')");
        echo "success"; 
    } else { 
        echo "error"; 
    } 
 ?> 
Sign up to request clarification or add additional context in comments.

5 Comments

I am connected to the database. I suppose this is what you mean: pastebin.com/niBAeyiW And that is what i tried and it doesn't work out.
If you're connected to database, and the path of the uploaded files is correct and you have write access on it and also if the query is correct then everything must be ok.. you can verify if the query is successfull like this: mysql_query(...your query...) or die(mysql_error());
It is uploading the files correctly, the only problem is the database insertion. The query does not generate any output (tried with the mysql_error), it's like it never sees it
I tried echoing a javascript popup where you've put in the SQL query, and it doesn't show either
Ok than try to put the query into a condition statement: if(mysql_query(...query...)) echo "success";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.