Jump to content

Help! PHP code showing all users info, not just logged in user?!


Recommended Posts

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.

 

 

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)

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! ::)

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

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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