html - How to upload images into MySQL database using PHP code -


i trying save images in database html form. have written php code accomplish task. program not generating error message, not inserting image data in mysql database. kindly check it. here sharing excerpt code.

        /*-------------------     image query      ---------------*/       $file   =$_files['image']['tmp_name'];     if(!isset($file))     {       echo 'please select image';     }     else      {        $image_check = getimagesize($_files['image']['tmp_name']);        if($image_check==false)        {         echo 'not valid image';        }        else        {         $image = file_get_contents ($_files['image']['tmp_name']);         $image_name = $_files['image']['name'];         if ($image_query = mysql_query ("insert product_images values (1,'$image_name',$image )"))         {           echo $current_id;          //echo 'successfull';         }         else         {           echo mysql_error();         }        }    }         /*-----------------     image query end     ---------------------*/      <form action='insert_product.php' method='post' enctype='multipart/form-data' ></br>             file        : <input type='file' name= 'image' >     </form> 

error message have error in sql syntax; check manual corresponds mysql server version right syntax use near '' @ line 1

firstly, should check if image column blob type!

i don't know sql table, if i'll try make own example.

we got fields id (int), image (blob) , image_name (varchar(64)).

so code should (assume id '1' , let's use mysql_query):

$image = addslashes(file_get_contents($_files['image']['tmp_name'])); //sql injection defence! $image_name = addslashes($_files['image']['name']); $sql = "insert `product_images` (`id`, `image`, `image_name`) values ('1', '{$image}', '{$image_name}')"; if (!mysql_query($sql)) { // error handling     echo "something went wrong! :(";  } 

you doing wrong in many ways. don't use mysql functions - deprecated! use pdo or mysqli. should think storing files locations on disk. using mysql storing images thought bad idea™. handling sql table big data images can problematic.

also html form out of standards. should this:

<form action="insert_product.php" method="post" enctype="multipart/form-data">     <label>file: </label><input type="file" name="image" />     <input type="submit" /> </form> 

sidenote:

when dealing files , storing them blob, data must escaped using mysql_real_escape_string(), otherwise result in syntax error.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -