1

I'm trying to upload an image using PHP to MySQL. In the query i'm trying to save the image and the directory of the image in the DB, but I get empty directory in the DB, plus no image in the folder. Any help would be greatly appreciated!

<?php
// include db connect class
 define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/android_connect/db_connect.php');

 $db = new DB_CONNECT();


//Setting up images directory
 $target = "./images"; 
 $target = $target . basename( $_FILES['photo']['name']);

 $photo=($_FILES['photo']['name']); 

//inserting data order
$order = "INSERT INTO scb
        (photo, directory)
        VALUES
        (  '$_POST[$target]',
        '({$_FILES['photo']['name']})')";  

 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information  has been added to the directory"; 
 } 
 else { 

 //Gives an error if its not 
 echo "Sorry, there was a problem uploading your file."; 
 } 

//declare in the order variable
$result = mysql_query($order);  //order executes
if($result){
echo "Thank you for submitting!";
} else{
echo "Sorry, something went wrong! Please try again!";
}
?>
5
  • 1
    Hey, Make sure you have appropriate access to the folder where you are trying to upload. Also you can get an error by printing out the value of: $_FILES["photo"]["error"] Commented Nov 1, 2014 at 21:27
  • Check for errors and also, check form (please provide form html, too). Commented Nov 1, 2014 at 21:31
  • 2
    I'm using an android app to upload the image, I've used another code to upload the image to the folder and it works, but when I wanted to save the Image to Mysql I get this error" Sorry, there was a problem uploading your file." which means that the image is not uploaded!I will try to print out the value of : $_FILES["photo"]["error"] and get back to you guys Commented Nov 1, 2014 at 21:35
  • just echo $_FILES['photo']['error'] in the else section. Commented Nov 1, 2014 at 21:47
  • 2
    No error showed up, when I tried! Commented Nov 1, 2014 at 23:18

2 Answers 2

1

Well first mysql_query is outdated use PDO instead (you'll have to check if this feature is enabled on your server by using phpinfo());

Try this

    <?php 
            //Connect to sql db
            try {
                    $user = "username";
                    $pass = "password";
                    $db = new PDO('mysql:host=yourhost;dbname=yourdb', $user, $pass);

            } catch (PDOException $e) {
                    print "Error!: " . $e->getMessage() . "<br/>";
                    die();
            }



            //Setting up images directory
            $target = "./images/";  //you forgot the last slash 
            $target = $target . basename($_FILES['photo']['name']);

            $photo = $_FILES['photo']['name']; 

            //inserting data order
            $stmt = $db->prepare("INSERT INTO scb (photo, directory) VALUES (:photo, :directory)");
            $stmt->bindParam(':photo', $_POST[$target]);
            $stmt->bindParam(':directory', $_FILES['photo']['name']);


            if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
            { 

                    //Tells you if its all ok 
                    echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information  has been added to the directory"; 
            } 
            else { 

                    //Gives an error if its not 
                    echo "Sorry, there was a problem uploading your file."; 
            } 

            //declare in the order variable
            if($stmt->execute()){
                    echo "Thank you for submitting!";
            } else{
                    echo "Sorry, something went wrong! Please try again!";
            }
    ?>
Sign up to request clarification or add additional context in comments.

2 Comments

Got this error Error!: SQLSTATE[42000] [1044] Access denied for user 'a5983957****'@'10.1.1.22' to database ' a5983957***' any idea how to deal with it?
Check your host, db name,usename,and password
0

Have you checked for any errors or warnings in the web server log files? Do you get any sensible output if you write:

print_r($_FILES);

Comments

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.