mysql - upload and retrieve images to database in php -


index.php:

<html> <head> <title>upload images</title> </head> <body>  <form action="index.php" enctype="multipart/form-data" method="post"> <table style="border-collapse: collapse; font: 12px tahoma;" border="1" cellspacing="5" cellpadding="5"> <tbody><tr> <td> <input name="uploadedimage" type="file"> </td> </tr> <tr> <td> <input name="upload now" type="submit" value="upload image"> </td> </tr> </tbody></table> </form>  <?php include("mysqlconnect.php");      function getimageextension($imagetype)      {        if(empty($imagetype)) return false;        switch($imagetype)        {            case 'image/bmp': return '.bmp';            case 'image/gif': return '.gif';            case 'image/jpeg': return '.jpg';            case 'image/png': return '.png';            default: return false;        }      }      if (!empty($_files["uploadedimage"]["name"])) {      $file_name=$_files["uploadedimage"]["name"];     $temp_name=$_files["uploadedimage"]["tmp_name"];     $imgtype=$_files["uploadedimage"]["type"];     $ext= getimageextension($imgtype);     $imagename=date("d-m-y")."-".time().$ext;     $target_path = "images/".$imagename;  if(isset($tmp_name)){ if(move_uploaded_file($tmp_name, $target_path)) {      $query_upload="insert 'images_tbl' ('images_path','submission_date') values   ('".$target_path."','".date("y-m-d")."')";     mysql_query($query_upload) or die("error in $query_upload ==".mysql_error());    }else{     exit("error while uploading image on server"); }  } } ?> </body> </html> 

mysqlconnect.php:

<?php /**********mysql settings****************/ $host="localhost"; $databasename="karma"; $user="root"; $pass=""; /**********mysql settings****************/ $conn=mysql_connect($host,$user,$pass); if($conn) { $db_selected = mysql_select_db($databasename, $conn); if (!$db_selected) {     die ('can\'t use foo : ' . mysql_error()); } } else {     die('not connected : ' . mysql_error()); } ?> 

when run above script, there no errors displayed. , no image didn't contain in database.

may know how upload , retrieve images database?

can me?

thanks in advance!!!

the problems causing code fail following:

change instances of $tmp_name in code $temp_name

this being if(isset($tmp_name)) , if(move_uploaded_file($tmp_name, $target_path))

since using $temp_name=$_files["uploadedimage"]["tmp_name"];, therefore using wrong variable, (by way) undefined.

also, stated in comments area; line:

insert 'images_tbl' ('images_path','submission_date') 

when referencing tables , columns, quotes not used. either wrap them in backticks, or remove them.

for example:

insert `tablename` (`column1`,`column2`) 

or

insert tablename (column1,column2) 

backticks used safeguard against using reserved words, or if there happens space between words used, or if hyphen used word seperator. (just quick fyi sidenote).

for list of reserved words, visit mysql.com website:

developer tips:

using proper error reporting vital during development.

adding following top of file(s) signal errors, if found:

error_reporting(e_all); ini_set('display_errors', 1); 

finally:

your present code open sql injection. use mysqli_* prepared statements, or pdo prepared statements.

examples can found inside pages.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -