FOXIT Posted September 15, 2014 Share Posted September 15, 2014 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 https://forums.phpfreaks.com/topic/291072-how-to-display-user-profile-image/ Share on other sites More sharing options...
ialsoagree Posted September 15, 2014 Share Posted September 15, 2014 Where is "$actual_image_name" being defined? Link to comment https://forums.phpfreaks.com/topic/291072-how-to-display-user-profile-image/#findComment-1491133 Share on other sites More sharing options...
FOXIT Posted September 15, 2014 Author Share Posted September 15, 2014 @ialsoagree: On line 22 of ajaximage.php Link to comment https://forums.phpfreaks.com/topic/291072-how-to-display-user-profile-image/#findComment-1491136 Share on other sites More sharing options...
ialsoagree Posted September 15, 2014 Share Posted September 15, 2014 Sorry, perhaps I should have been more specific. Where is "$actual_image_name" being defined in "student_home.php"? Link to comment https://forums.phpfreaks.com/topic/291072-how-to-display-user-profile-image/#findComment-1491138 Share on other sites More sharing options...
FOXIT Posted September 15, 2014 Author Share Posted September 15, 2014 @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. Link to comment https://forums.phpfreaks.com/topic/291072-how-to-display-user-profile-image/#findComment-1491155 Share on other sites More sharing options...
ialsoagree Posted September 15, 2014 Share Posted September 15, 2014 $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']; Link to comment https://forums.phpfreaks.com/topic/291072-how-to-display-user-profile-image/#findComment-1491200 Share on other sites More sharing options...
ialsoagree Posted September 15, 2014 Share Posted September 15, 2014 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 https://forums.phpfreaks.com/topic/291072-how-to-display-user-profile-image/#findComment-1491208 Share on other sites More sharing options...
FOXIT Posted September 16, 2014 Author Share Posted September 16, 2014 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 https://forums.phpfreaks.com/topic/291072-how-to-display-user-profile-image/#findComment-1491228 Share on other sites More sharing options...
mac_gyver Posted September 16, 2014 Share Posted September 16, 2014 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 https://forums.phpfreaks.com/topic/291072-how-to-display-user-profile-image/#findComment-1491229 Share on other sites More sharing options...
FOXIT Posted September 17, 2014 Author Share Posted September 17, 2014 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 56Table 'student.users' doesn't exist Link to comment https://forums.phpfreaks.com/topic/291072-how-to-display-user-profile-image/#findComment-1491352 Share on other sites More sharing options...
FOXIT Posted September 17, 2014 Author Share Posted September 17, 2014 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 https://forums.phpfreaks.com/topic/291072-how-to-display-user-profile-image/#findComment-1491353 Share on other sites More sharing options...
mac_gyver Posted September 17, 2014 Share Posted September 17, 2014 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; ?>" /> Link to comment https://forums.phpfreaks.com/topic/291072-how-to-display-user-profile-image/#findComment-1491366 Share on other sites More sharing options...
FOXIT Posted September 18, 2014 Author Share Posted September 18, 2014 Thank you so much mac_gyver..my long time nightmare has been solved. Link to comment https://forums.phpfreaks.com/topic/291072-how-to-display-user-profile-image/#findComment-1491461 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.