0

I'm having quite a lot of problems uploading images to a folder and inserting the corresponding filepath into the database.

That's what i've tried:

$target_dir = "img/posts";
$target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["imageUpload"]["name"]). " has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}

$image=basename( $_FILES["imageUpload"]["name"],".jpg"); // used to store the filename in a variable

//storind the data in your database
$query= "INSERT INTO posts `post_image` VALUES ('$image')";
mysql_query($query);

How should I proceed?

2
  • Have you tried debugging your code? Does $image actually have anything in it? Do you have a DB connection file set up? You also should be using MySQLi or PDO instead of mysql_* functions as they are deprecated technology Commented Apr 20, 2016 at 10:00
  • And what is your problem exactly? Any error? Commented Apr 20, 2016 at 10:00

4 Answers 4

2

Insert query syntax is

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

Write Column inside ()

$query= "INSERT INTO posts (`post_image`) VALUES ('$image')";

                          ^^^^         ^^^

NOTE:- mysql is deprecated instead use mysqli Or PDO

Sign up to request clarification or add additional context in comments.

Comments

0

update your insert query:

$query= "INSERT INTO posts (`post_image`) VALUES ('$image')";

Also print this query to check whether $image has value or not.

2 Comments

what is the output of the insert query...just print it in browser or let us know the error
print $_FILES before insert also.
0

You can use this code

$allow = array("jpg", "jpeg", "gif", "png");

$todir = 'img/posts/';

if ( $_FILES['imageUpload']['tmp_name'] ) // is the file uploaded yet?
{
    $filename = str_replace(array("(",")"), "-", str_replace(" ", "_", $_FILES['imageUpload']['name']));
    $filename_parts = pathinfo($filename);
    $filename = $filename_parts['filename'].'_'.time().'.'.$filename_parts['extension'];

    $info = explode('.', strtolower( $_FILES['imageUpload']['name']) ); // whats the extension of the file

    if ( in_array( strtolower(end($info)), $allow) ) // is this file allowed
    {
        $photo_file = $filename;


            if ( move_uploaded_file( $_FILES['imageUpload']['tmp_name'], $todir . basename($filename ) ) )
            {
                // the file has been moved correctly
                // Registration Success
                $query= "INSERT INTO posts (`post_image`) VALUES ('$photo_file')";
                mysql_query($query);
                echo "The file ". $photo_file. " has been uploaded.";
            }
            else
            {
                // uploading error
                echo 'Error: while file uploading but data has been saved';
            }

    }
    else
    {
        // error this file ext is not allowed
        echo 'Error: file ext is not allowed';
    }
}

2 Comments

Thanks Rohan H! The file upload works, but it's still isn't inserting into the table...
can you...... echo $query= "INSERT INTO posts (post_image) VALUES ('$photo_file')";exit; and run the code and copy pest that query in MySql and see if that runs or not
0

Here is a sample of upload i tested and it works

<form enctype="multipart/form-data" method="post" action="">

<input type="file" name="imageUpload" >
<input type="submit" name="submit" >
</form>

php code

if(isset($_POST['submit']))
 {
   $target_dir = "upload/";
   $target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);
   $uploadOk = 1;
   $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

   if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["imageUpload"]["name"]). " has been uploaded.";
   } else {
    echo "Sorry, there was an error uploading your file.";
   }

   $image=basename( $_FILES["imageUpload"]["name"],".jpg"); // used to store the filename in a variable

  //storind the data in your database
  $query= "INSERT INTO posts (`post_image`) VALUES ('$image')";
  $result=mysql_query($query);
  if($result){
    echo "success";
  }else{
    echo "failure";
  }
}

If this is not working then check your database connection

NOTE:- mysql is deprecated instead use mysqli Or PDO

3 Comments

"Where did this $image variable comes from?" Says it in the code $image=basename( $_FILES["imageUpload"]["name"],".jpg");
@ Andy Holmes sorry i didnt see that any way thanks i updated that
@SanoojT The form tag is correct, made the amendments to the INSERT but still no change..

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.