php - how to retrieve image from database using mysqli prepare? -


i'm trying convert codes mysqli prepare , i'm having trouble retrieving images database particular data. let example, id 1 post image of in tha same page.

can please check codes guys? i'm trying view image particular data. can me guys? thank you, code:

this code in form:

<form action="upload_photo.php" method="post" enctype="multipart/form-data">     <div class="col-md-6">         <div class="alert alert-info" role="alert"><span class="glyphicon glyphicon-user" aria-hidden="true"></span>&nbsp;&nbsp;add/update photo</div>         <input type="hidden" name="id" value="<?= $id; ?>" />         <input type="file" name="image"><br>         <input type="submit" value="upload image" name="submit">     </div>     <div class="row">         <div class="col-md-6 col-md-3">             <a href="#" class="thumbnail">             <img src="image_view.php?id=$id" alt="...">             </a>         </div>             updated photo      </div> </form> 

this code php upload image:

<?php  include '../session.php'; require_once 'config.php';   if (isset($_post['submit'])) {      $imagename = mysqli_real_escape_string($conn, $_files["image"]["name"]);     $imagedata = mysqli_real_escape_string($conn, file_get_contents($_files["image"]["tmp_name"]));     $imagetype = mysqli_real_escape_string($conn, $_files["image"]["type"]);          if (substr($imagetype, 0,5) == "image") {             $query = "update `crew_info` set `image_name` = ?, `updated_photo` = ? `id` = ?";             $stmt = mysqli_prepare($conn, $query);             mysqli_stmt_bind_param($stmt, 'ssi', $imagename, $imagedata, $_post['id']);             mysqli_stmt_execute($stmt);             $id = $_post['id'];              header("location: ../admin/view_all_info.php?id=$id");          }          else {              echo "image not uploaded!";          }  }  ?> 

this code retrieving image not working me:

<?php  include '../session.php'; require_once 'config.php';  if (isset($_post['id'])) {      $id = mysqli_real_escape_string($_post['id']);     $query = "select * `crew_info` `id` = ?";     $stmt = mysqli_prepare($conn, $query);     mysqli_stmt_bind_param($stmt, 's', $_post['id']);     mysqli_stmt_execute($stmt);     mysqli_stmt_bind_result($stmt, $id, $first_name, $middle_name, $last_name, $age, $month, $day, $year, $birth_place, $gender, $martial_status, $religion, $nationality, $email_address, $address_1, $address_2, $course, $school_graduated, $remarks, $date_added, $crew_status, $image_name, $updated_photo);      while (mysqli_stmt_fetch($stmt)) {          sprintf("%s", $updated_photo);     }      header("content-type: image/jpeg");     sprintf("%s", $updated_photo);  } else {     echo "bad"; }    ?> 

the image uploading cannot retrieve it. please me guys thank you

there few problems here:

  1. in html use <img src="image_view.php?id=$id" alt="...">. makes request server, need $_get['id'] instead of $_post['id'] in script output image.
  2. you should not use mysqli_real_escape_string() when use prepared statement, mysqli take care of when execute prepared statement.
    additionaly, in script use image, not sending database connection first parameter, make fail well. edit: don't seem using escaped value can remove line.

apart should probably:

  • store image file in file-system. if content not sensitive / protected, can put somewhere in root of web-server can include link it. make database maintenance , backups lot easier.
  • only need when display image if use method using now. make code easier read , new columns not introduce bugs when do:
    select updated_photo crew_info id = ?.

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo