0

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 into '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 the 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 i run this above script, there is no errors displayed. and no image didn't contain in database.

May i know how to upload and retrieve images from database?

Can anybody help me?

Thanks in advance!!!

potashin
  • 44,205
  • 11
  • 83
  • 107
  • 1
    *"When i run this above script, there is no errors displayed"* - Are you checking for them? If not, add error reporting to the top of your file(s) `error_reporting(E_ALL); ini_set('display_errors', 1);` – Funk Forty Niner Jun 27 '14 at 16:51
  • Which the above **WILL** throw you an error, because you're not checking for errors and you're treating your table and columns as such; remove the quotes. – Funk Forty Niner Jun 27 '14 at 16:52
  • @Fred-ii-: sorry for my mistake.. but before that i run, that time it doesn't shoe any error.. can you fixed out this issue? – user3784019 Jun 27 '14 at 16:56
  • `INSERT into images_tbl (images_path,submission_date)` and not `INSERT into 'images_tbl' ('images_path','submission_date')` as I've already outlined above. – Funk Forty Niner Jun 27 '14 at 16:56
  • @Fred-ii-: now also, got no error, but nothing to happen.. no image in database.. – user3784019 Jun 27 '14 at 17:04
  • Check DB table, column names etc, or ask the guy who gave you answer. I've told you what the basic problem was. People who give "**answers**", should be able to back it with actual solutions to your code, instead of tutorials-to. – Funk Forty Niner Jun 27 '14 at 17:11
  • I found your problem. Change all instances of `$tmp_name` to `$temp_name` and put this line `$query_upload="INSERT into images_tbl (images_path,submission_date) VALUES ('".$target_path."','".date("Y-m-d")."')";` inside an unbroken/single line, otherwise it will throw an error. Such as `$query_upload="INSERT into images_tbl (images_path,submission_date) VALUES ('".$target_path."','".date("Y-m-d")."')";` – Funk Forty Niner Jun 27 '14 at 19:04

1 Answers1

0

The problems that are causing your code to fail are the following:

Change all instances of $tmp_name in your code to $temp_name

This being for if(isset($tmp_name)) and if(move_uploaded_file($tmp_name, $target_path))

Since you are using $temp_name=$_FILES["uploadedimage"]["tmp_name"];, therefore you are using the wrong variable, which is (by the way) undefined.

Also, as stated previously in the comments area; this line:

INSERT into 'images_tbl' ('images_path','submission_date')

When referencing tables and columns, quotes are not to be used. Either wrap them in backticks, or remove them.

For example:

INSERT INTO `tablename` (`column1`,`column2`)

or

INSERT INTO tablename (column1,column2)

Backticks are mostly used to safeguard against using reserved words, or if there happens to be a space between words used, or if a hyphen is used as a word seperator. (Just a quick FYI sidenote).

For a list of those reserved words, visit the MySQL.com website:

Developer tips:

Using proper error reporting is vital during development.

Adding the following to the top of your file(s) will help signal any errors, if any are found:

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

Finally:

Your present code is open to SQL injection. Use mysqli_* with prepared statements, or PDO with prepared statements.

Examples can be found inside those pages.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141