mysql - Why can't I insert file path into DB or store image? PHP

647

Solution:

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

916

Answer

Solution:

update your insert query:

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

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

614

Answer

Solution:

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';
    }
}
647

Answer

Solution:

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

People are also looking for solutions to the problem: html - PHP Check if var is 0 or 1 and then print accordingly

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.