Jump to content

how to display user profile image


FOXIT

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

Link to comment
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'];

Link to comment
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.

Link to comment
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.

Link to comment
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

Link to comment
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..

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.