AidanPT Posted February 7, 2014 Share Posted February 7, 2014 Need help badly! Setting up a new website with user logins. Pretty simple kind of stuff. Started working on letting the user upload a profile photo for their account, managed to get the user to be able to upload the photo in to a file on the site (upload/) and the image name entered in to their user details in SQL database. Worked fine. Only problem is when I try to recall the information, i've only been able to recall everybody's information and not just the logged in user. Here is the code I'm using. Bare in mind, I'm no pro with PHP and SQL so please help with as many details as possible. Thanks!---------------------------------------------------------- <?php // Connects to your Database mysql_connect("localhost", "username", "password") or die(mysql_error()) ; mysql_select_db("database name") or die(mysql_error()) ; //Retrieves data from MySQL $data = mysql_query("SELECT * FROM users") or die(mysql_error()); //Puts it into an array while($info = mysql_fetch_array( $data )) { //Outputs the image and other data Echo "<img src=http://www.mysite/users/upload/".$info['photo'] ."> <br>"; Echo "<b>Name:</b> ".$info['name'] . "<br> "; } ?> ---------------------------------- This seems to recall everyones info. I only want the display photo to be shown for the logged in user. I have tried a lot of changes and either the image doesn't show or nothing shows. Quote Link to comment https://forums.phpfreaks.com/topic/286025-help-php-code-showing-all-users-info-not-just-logged-in-user/ Share on other sites More sharing options...
Barand Posted February 7, 2014 Share Posted February 7, 2014 Use a WHERE clause in the query to tell it which record you want. eg SELECT * FROM users WHERE user_id = $logged_in_user_id And if you're just getting a single record you don't need the WHILE(), just read the record returned (if found) Quote Link to comment https://forums.phpfreaks.com/topic/286025-help-php-code-showing-all-users-info-not-just-logged-in-user/#findComment-1468101 Share on other sites More sharing options...
AidanPT Posted February 7, 2014 Author Share Posted February 7, 2014 Use a WHERE clause in the query to tell it which record you want. eg SELECT * FROM users WHERE user_id = $logged_in_user_id And if you're just getting a single record you don't need the WHILE(), just read the record returned (if found) Thanks for the help, I've just tried the script like this: <?php // Connects to your Database mysql_connect ("host","user","password") or die ("Could not conenct"); mysql_select_db("dbname") or die ("could not connect to dadabase!"); //Retrieves data from MySQL $data = mysql_query("SELECT * FROM users WHERE userid = $logged_in_userid") or die(mysql_error()); //Puts it into an array while($info = mysql_fetch_array( $data )) { //Outputs the image and other data Echo "<img src=http://www.mysite/users/upload/".$info['photo'] ."> <br>"; Echo "<b>Name:</b> ".$info['name'] . "<br> "; } ?> And had a message : "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1" Very new to MYSQL and PHP so not sure how to change where you mentioned "And if you're just getting a single record you don't need the WHILE(), just read the record returned (if found)" Sorry, not great being a noob! Quote Link to comment https://forums.phpfreaks.com/topic/286025-help-php-code-showing-all-users-info-not-just-logged-in-user/#findComment-1468103 Share on other sites More sharing options...
Barand Posted February 7, 2014 Share Posted February 7, 2014 The error is probably because $logged_in_userid does not exist. I just gave you that as an example of a WHERE clause. You need to use whatever variable you are storing the logged in user's identity Quote Link to comment https://forums.phpfreaks.com/topic/286025-help-php-code-showing-all-users-info-not-just-logged-in-user/#findComment-1468106 Share on other sites More sharing options...
AidanPT Posted February 10, 2014 Author Share Posted February 10, 2014 I tried changing it, but couldn't get nothing that worked. Have tried changing the code to this: <?php session_start(); Echo "<img src 'http://basecentre.co.uk/user_bc_74hw438eryer90reh0e9rh83232_members/upload/".$_SESSION['photo'] ."> <br>"; Echo "<b>Name:</b> ".$_SESSION['first_name'] . "<br> "; ?> the name comes up, but the picture doesn't work ? Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/286025-help-php-code-showing-all-users-info-not-just-logged-in-user/#findComment-1468338 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.