Jump to content
Sign in to follow this  
FOXIT

how to display user profile image

Recommended Posts

Good Day PHP world,

 

I am encountering a problem in php code meant to allow the user to update their profile picture.

I am using jquery.min and jquery.js. The code below runs with no errors reported. The file has been successfully uploaded to upload path using this form.

upload.php

<form  id="imageform" method="post" enctype="multipart/form-data" action='ajaximage.php'>
<input type="file" name="photoimg" id="photoimg" class="stylesmall"/>
</form>

ajaximage.php

$path = "uploads/";
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024)) // Image size max 1 MB
{
$actual_image_name = $name.".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
$query   = "UPDATE users SET profile_image='$actual_image_name' WHERE student_id='{$_SESSION['user_id']}'";
$result = mysqli_query($link_id, $query);
echo "<img src='uploads/".$actual_image_name."' class='preview'>";
}

The problem is the image being uploaded does not display on the

Student_home.php

<div id="about-img">
    <img class="profile-photo" align="middle" src='uploads/".$actual_image_name."' />
</div>

But the image uploaded will display when i write directly its filename example

<div id="about-img">
    <img class="profile-photo" align="middle" src="uploads/107.jpg" />
</div>

My problem is i wanted to display the uploaded picture of the specific student on Student_Home.php

Share this post


Link to post
Share on other sites

Sorry, perhaps I should have been more specific.

 

Where is "$actual_image_name" being defined in "student_home.php"?

Share this post


Link to post
Share on other sites

@ialso agree: whoahh i am not defining it in Student_Home.php. Do i need to do it? Sorry, can you teach me how to define it.

Share this post


Link to post
Share on other sites

$query = "SELECT u.profile_image FROM users AS u WHERE student_id = $current_student_profile_id";
$result_array = mysql_fetch_assoc(msyqli_query($link_id, $query)); // You'll have to define $link_id yourself
$actual_image_name = $result_array['profile_image'];

Share this post


Link to post
Share on other sites

Sorry, I forgot my "i" above for mysqli_fetch_assoc!

 

$query = "SELECT u.profile_image FROM users AS u WHERE student_id = $current_student_profile_id";
 
$result_array = mysqli_fetch_assoc(msyqli_query($link_id, $query)); // You'll have to define $link_id yourself

$actual_image_name = $result_array['profile_image'];

Edited by ialsoagree

Share this post


Link to post
Share on other sites

I have followed your advise. Please take a look on my student_home.php

<?php
$query = "SELECT users.profile_image FROM users AS users WHERE student_id='{$_SESSION['user_id']}'";
$result_array = mysqli_fetch_assoc(mysqli_query($link_id, $query));
$actual_image_name = $result_array['profile_image'];
?>
<div id="about-img">
    <img class="profile-photo" align="middle" src='uploads/".$actual_image_name."' />
</div> 

regarding on the definition of link_id. here is my connect.php

<?php
	$host = "localhost";
	$dbusername = "root";
	$dbpassword = "765632";
	$dbname = "student";
	$link_id = mysqli_connect($host,$dbusername,$dbpassword,$dbname) or die("Error " . mysqli_error($link_id));	
?>

I now received a display warning saying

 

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\xamppp\htdocs\a\Student_Home.php on line 56

and the profile image still wont display.

Share this post


Link to post
Share on other sites
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in

 

 

^^^ that's not the problem. that is a follow-on error. it is caused by a query that failed due to an error of some kind and your code didn't stop the rest of the code, the mysqli_fetch_assoc() statement, from trying to use the result from the failed query.

 

to find out why the query failed, you can echo mysqli_error($link_id); after the line where you used the mysqli_query() statement.

Share this post


Link to post
Share on other sites

thanks mac_gyver found the error by following your suggestion.

 

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\xamppp\htdocs\a\Student_Home.php on line 56
Table 'student.users' doesn't exist

Share this post


Link to post
Share on other sites

i have recoded my page to make the uploaded image save also in the database rather than just in path folder with a renamed file of student id. And it went great the only problem left is in displaying it. Here is my student_home.php

<?php
$query = "SELECT image FROM student_information WHERE student_id='{$_SESSION['user_id']}'";
$result_array = mysqli_fetch_assoc(mysqli_query($link_id, $query));
$actual_image_name = $result_array['image'];
?>
<div id="about-img">
    <img class="profile-photo" align="middle" src='uploads/".$actual_image_name."' />
</div>

The image wont display using the variable 

$actual_image_name

but will display using directly the filename, example

src="uploads/125.jpg"

any idea, would be helpful..

Share this post


Link to post
Share on other sites

in the html <img ...> tag, you need to actually have php code that echos the variable -

<img class="profile-photo" align="middle" src="uploads/<?php echo $actual_image_name; ?>" />

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.