1

I need to upload an image to database and path should be saved in table. I am using the below code to upload the image and store the path in database table. I have created a directory "images" for storing image which is uploaded by user.

if(isset($_POST['submit']))
{
  $errors= array();
  $file_name=$_FILES['photo']['name'];
  $file_size =$_FILES['photo']['size'];
  $file_tmp =$_FILES['photo']['tmp_name'];
  $file_type=$_FILES['photo']['type'];
  $file_ext=strtolower(end(explode('.',$_FILES['photo']['name'])));
  $expensions= array("jpeg","jpg","png");
  if(in_array($file_ext,$expensions)=== false){
  $errors[]="extension not allowed, please choose a JPEG or PNG file.";
  }
  if($file_size > 2097152){
  $errors[]='File size must be excately 2 MB';
  }
  if(empty($errors)==true){
  move_uploaded_file($file_tmp,"images/".$file_name);
  echo '<script language="javascript">';
  echo 'alert("Success")';
  echo '</script>';
  }
  else
  {
    echo '<script language="javascript">';
    echo 'alert("Failed")';
    echo '</script>';
    print_r($errors);
  }
  }
  $query = mysql_query("insert into e(photo) values ('$file_name')");
  echo '<script language="javascript">';
  echo 'alert("Ok")';
  echo '</script>';
 }

Then the image should be displayed from database using the stored path in table. I am using the below code to display the image from database.

$connection = mysql_connect("localhost", "root", "");
$db = mysql_select_db("s", $connection);
$sql = "SELECT * FROM e";
$result = mysql_query($sql);
while ($o = mysql_fetch_assoc($result))
{
    echo "<tbody>";
    echo "<tr>";
    echo "<td>".'<img src="$photo;"' ."</td>";
    echo "</tr>";
    echo "</tbody>";
}
?>
6
  • what is the problem?? Commented Mar 30, 2017 at 10:54
  • The table contains only file name i.e image name. The image is not displayed while using the select query which is showed above. Commented Mar 30, 2017 at 10:57
  • what is the value of $MyPhoto? Commented Mar 30, 2017 at 10:58
  • Sorry it is $photo it is the table column name. It is not displaying the image. Commented Mar 30, 2017 at 11:04
  • Little Bobby says your script is at risk for SQL Injection Attacks.. Even escaping the string is not safe! Commented Mar 30, 2017 at 14:31

2 Answers 2

2

Here you are making mistake

echo "<td>".'<img src="$photo;"' ."</td>";

Replace it with (Ensure that you are completing img tag)

echo '<td><img src="your_images_folder_path/'.$o['photo'].'"></td>';

Please stop using mysql_* it's depreciated since PHP5.6

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

3 Comments

Thank You but I am using old version in my system.
Yes now it is displaying
Great! Happy coding! :D
0
$connection = mysql_connect("localhost", "root", "password", "s");
$sql = "SELECT * FROM e";
$result = mysql_query($sql);
while ($o = mysql_fetch_assoc($result))
{
    echo "<tbody>";
    echo "<tr>";
    echo "<td>".'<img src="path/'.$o['photo']."></td>";
    echo "</tr>";
    echo "</tbody>";
}
?>

Where photo is the column name from your database. Also, you can simply pass the database name in your connection declaration instead of using the mysql_select_db

7 Comments

There is an error in the following statement. I think single quotation is missing.
Buddy still image is not displaying.
it wont display because you have to enter your path. I do not know where you save your images. Change that line to match your structure
echo "<td>".'<img src="images/'.$o['photo']."></td>";
This is not displaying
|

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.