Click here to Skip to main content
14,698,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
why did my image column in phpmyadmin database is not inserting the filename of the image i inserted but instead it displays [BLOB - 10 B]? and when i'm trying to retrieve or display the inserted image, it displays a blank square box and not the image itself.

What I have tried:

i tried to use the .base64 code to retrieve and display the image but same result, still that blank square box appears and not the inserted image:

here's my code (inserting image)
<?php
session_start();
 error_reporting( ~E_NOTICE ); // avoid notice
$servername = "localhost";
$username = "bctixpmy_blake";
$password = "Ae1IkMMGj6d2";
$dbname = "bctixpmy_luxury_db";

  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 if(isset($_POST['btnsave']))
 {//1
$agent_id = rand(10,1000);
$fullname = $_POST['full_name'];
$username = $_POST['user_name'];
$useremail = $_POST['user_email'];
$userweb = $_POST['user_web'];
$userpword = $_POST['user_pword'];
$usercpword = $_POST['user_cpword'];
$stat = 'ACTIVE';
//
$sql = "SELECT * from tbl_brokers WHERE username='$username'";
if ($res = $conn->query($sql)) {//2

if ($res->fetchColumn() > 0) {//3
echo '<script language="javascript">';
echo 'alert("That Username already exist!");';
echo 'location.href="https://luxury.global/broker-signup/";';
echo '</script>';
}
else if($userpword != $usercpword)
{
echo '<script language="javascript">';
echo 'alert("Password and Confirm Password did not match.");';
echo 'location.href="https://luxury.global/broker-signup/";';
echo '</script>';
}
else{
//
  $imgFile = $_FILES['user_image']['name'];
  $tmp_dir =$_FILES['user_image']['tmp_name'];
  $imgSize =$_FILES['user_image']['size'];

  if(empty($imgFile)){
  $errMSG = "Please Select Image File for Profile Picture.";
echo '<script language="javascript">';
echo 'alert("'.$errMSG .'");';
echo 'location.href="https://luxury.global/broker-signup/";';
echo '</script>';
  }
  else
  {
   $upload_dir = 'user_images/'; // upload directory
 
   $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
  
   // valid image extensions
   $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
  
   // rename uploading image
   $userpic = rand(1000,1000000).".".$imgExt;
    
   // allow valid image file formats
   if(in_array($imgExt, $valid_extensions)){   
    // Check file size '5MB'
    if($imgSize < 5000000)    {
     move_uploaded_file($tmp_dir,$upload_dir.$userpic);
    }
    else{
     $errMSG = "Sorry, your file is too large.";
echo '<script language="javascript">';
echo 'alert("'.$errMSG .'");';
echo 'location.href="https://luxury.global/broker-signup/";';
echo '</script>';
    }
   }
   else{
    $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";  
echo '<script language="javascript">';
echo 'alert("'.$errMSG .'");';
echo 'location.href="https://luxury.global/broker-signup/";';
echo '</script>';
   }
  }
  
  
  // if no error occured, continue ....
  if(!isset($errMSG))
  {
   $stmt = $conn->prepare('INSERT INTO tbl_brokers(agent_id, fullname, username, user_email, website, pword, conf_pword, userPic, filename, Status) VALUES(:agentid, :fullname, :uname, :user_email, :user_web, :user_pword, :user_cpword, :upic, :imgFile, :stat)');
   $stmt->bindParam(':agentid',$agent_id);
   $stmt->bindParam(':fullname',$fullname);
   $stmt->bindParam(':uname',$username);
   $stmt->bindParam(':user_email',$useremail);
   $stmt->bindParam(':user_web',$userweb);
   $stmt->bindParam(':user_pword',$userpword);
   $stmt->bindParam(':user_cpword',$usercpword);
   $stmt->bindParam(':upic',$userpic);
   $stmt->bindParam(':imgFile',$imgFile);
   $stmt->bindParam(':stat',$stat);
   if($stmt->execute())
   {
    $successMSG = "New Broker Record has been successfully saved!";
echo '<script language="javascript">';
echo 'alert("'.$successMSG .'");';
echo 'location.href="https://luxury.global/broker-log-in/";';
echo '</script>';
    
   }
   else
   {
    $errMSG = "error while inserting....";
echo '<script language="javascript">';
echo 'alert("Error while saving records...);';
echo 'location.href="https://luxury.global/broker-signup/";';
echo '</script>';
   }
}
}
  }
 }
?>



and also, i cannot retrieve and display the i inserted using the above code,it only displays a blank square box. if you don't mind, can you give me a sample php script to display images from the database? thank you
Posted
Updated 16-Oct-20 1:12am
v2
Comments
Richard Deeming 16-Oct-20 4:29am
   
There's a secret error somewhere in your secret code. You should fix that.

Seriously, if you want someone to help you fix your code, you need to show the relevant parts of your code - in this case, the code that inserts the image into your database. Click the green "Improve question" link and add the relevant parts of your code to the question.
Member 11241887 16-Oct-20 6:13am
   
i updated my question and paste my code. hope you can help me. thank you :-)
Richard MacCutchan 16-Oct-20 7:48am
   
Which part is saving the image, and which part is reading it back from the database?
Member 11241887 22-Oct-20 20:23pm
   
the insert query and the move_upload function.thank you
Richard Deeming 16-Oct-20 9:38am
   
NB: You're storing the user's password in plain text. Don't do that.
Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

PHP even has built-in functions to help you do the right thing:
PHP: password_hash[^]
PHP: password_verify[^]

And you don't need to store both the "password" and "confirm password" values - you should be validating that they're the same value before creating the record, so you're just duplicating data.
rommelpabustan 16-Oct-20 10:16am
   
Thank you for the tip

1 solution

Your insert most likely failed, quite possibly for the reason stated in Why do I get a "Parameter is not valid." exception when I read an image from my database?[^].
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900