2

I need to upload an image in mysql database and it should be displayed in table when it is required. I am using the below code to store in database.

<input type="file" name="photo" id="photo">

<input type="submit" value="Save" name="submit">

<?php
    mysql_connect("localhost", "root", ""); mysql_select_db("prs");
    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);
    }
    }
    $photo = $_FILES['photo'];
    {   
      $query = mysql_query("insert into e (photo) values ('$photo')");
      echo '<script language="javascript">'; echo 'alert("Employed")';
      echo '</script>';
    }
    else
    {
     echo'<script language="javascript">';echo 'alert("Insertions Failed")';
     echo '</script>';   
    }
}
    mysql_close($connection);
?>

The code below is used to display the image from database on form load.

<form method="get" enctype="multipart/form-data">
              <table class="table table-bordered table-striped">
              <thead><tr><th>Photo</th></tr></thead>
              <tbody><tr>
                  <?php
                $connection = mysql_connect("localhost", "root", "");
                $db = mysql_select_db("prs", $connection);
                $sql = "SELECT * FROM e";
                $result = mysql_query($sql);
                while ($outw = mysql_fetch_assoc($result))
                {
                     echo "<tbody>";
                     echo "<tr>"; echo "<td>".'<img src="data:image/jpeg;base64,'.base64_encode( $result['photo'] ).'"/>' ."</td>"; echo "</tr>";
                     echo "</tbody>";
                }
                ?>
                </tr> 
              </tbody>
            </table>
            </form>

I am using blog as datatype for photo. The above code is not displaying the image from database.

Developer
  • 73
  • 7
  • 1
    why don't you break down the process into pieces to see where the problem is - ie write unit tests or just use print statements to log db connection, image retreival, process of displaying something to screen etc.. – Don Smythe Mar 25 '17 at 07:22
  • Success message is displayed after submit button is clicked also the image is stored in local folder images but the image is not displaying. @DonSmythe – Developer Mar 25 '17 at 07:36
  • before you add to database $photo = $_FILES['photo']; try print_r($photo = $_FILES['photo']); –  Mar 25 '17 at 10:09
  • stop using **`mysql_*`** extension , it's deprecated and will be hard to get support to it , us [mysqli_*](http://php.net/mysqli) or [pdo](http://php.net/pdo) instead – hassan Mar 25 '17 at 10:11

1 Answers1

1

Here you need get image data and save in database as BLOB:

$photo = file_get_contents($_FILES['photo']['tmp_name']);

Can I store images in MySQL

Community
  • 1
  • 1
  • how connect with php pdo to mysql https://github.com/fxstar/PhpJsCss/blob/master/PDOapi/addmsg.php –  Mar 25 '17 at 10:22