Becky Becky - 1 year ago 64
MySQL Question

Output of select query issue

In the query below I am attempting to select the user's profile picture and output it. My insert query and select query is working (I believe). Right now, I can upload the image and the user's user_id is associated with the image in my database. Then in my select query I am attempting to select the image associated with that user.

My database for this is simple and only has the following within it...

id, user_id, img

I believe the issue may reside with my output, here:

<img id="profile-pic" src="<?php echo (!empty($profPic) && $profPic != 0)? $profPic['img'] : "profile_images/default.jpg"; ?>" alt="<?php echo (!empty($profPic) && $profPic != 0)? "Profile Picture" : "No Picture"; ?>" />

I just don't know what I am doing wrong. Does anyone see if I am outputting this wrong? Right now, the default image loads upon page load, however, once I upload an image for a user it displays, but if I reload the page, the image goes back to the default.

function getPhoto($con,$dest)
$user_id = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" ); //added

$result = mysqli_query($con,"SELECT * FROM `profile_img` where `user_id` = '$user_id' ORDER BY `id` DESC LIMIT 1");

if($row = mysqli_fetch_array($result))
return $row;

return 0;

// Make sure all functions above are include here

// Get the database connection
$con = Connection();
// Check for post
if(isset($_POST['create'])) {
// Try uploading
$upload = UploadFile($_FILES);
// If upload fails
echo '<h3>Sorry, an error occurred</h3>';
else {
// You could add error handling here based on the results of
// each function's success or failure below.

// Try to save it
$saveToDb = SaveToDb($con,$upload['file']['dest']);
// Get the profile from image name
$profPic = ($saveToDb)? getPhoto($con,$upload['file']['dest']) : false; ?>

<img id="profile-pic" src="<?php echo (!empty($profPic) && $profPic != 0)? $profPic['img'] : "profile_images/default.jpg"; ?>" alt="<?php echo (!empty($profPic) && $profPic != 0)? "Profile Picture" : "No Picture"; ?>" />
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="file" class="inputbarfile" onchange="readURL(this);">
<img width="400px" height="300px" id="file" src="#" alt="your image">
<input type="submit" name="create" id="signinButton" value="Upload">

Answer Source

Calling the $profPic the way you called it would not allow for the data to be shown on page refresh because on page refresh no data is inserted.

Yea you can just change it to:

$profPic = getPhoto($con,$dest);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download