public/private user profiles- id help

My current user profiles only show the private section, ie for users to change their username or password or email. I’d like it so there is a public profile other users can view, so the usual myprofile.php?id=1 public profile. Here’s the original profile code:
[php]<?php

session_start();

include(‘config.php’);
include(‘date.php’);

$sql = mysql_query( “SELECT * FROM users WHERE id=’”.$_SESSION[‘id’]."’" );

$userfinal = $_SESSION[‘id’];

$user = $userfinal;

$id = $_GET[‘id’];

echo "

Profile

";

$row = mysql_fetch_array($sql);
echo “






";

echo "

ID#: ”.$user."
Name: “.$row[‘callname’].”
Email: “.$row[‘email’].”
Password: <input type=‘password’ value=’".$row[‘password’]."’ disabled=‘true’ />
Registered: “.$row[‘registered’].”
Last Login: “.$row[‘lastlogin’].”


";

?>

<?php include('footer.php'); ?>

[/php]

And with the editprofile.php it works fine. However I tried changing it so it would display a public version others could see, here’s my revisions:
[php]<?php

session_start();

include(‘config.php’);
include(‘date.php’);

$id = $_GET[‘id’];
$sql = mysql_query(“SELECT * FROM members WHERE id = ‘$id’ LIMIT 1”);
$check = mysql_num_rows($sql);

if ($check > 1)
{
echo “No one matches that id number!”;
exit();
}

	if($check == 1)
	{

while($row = mysql_fetch_array($sql))
{

$user = $id;

echo "

Profile

";

$row = mysql_fetch_array($sql);
echo “






";

echo “

ID#: ”.$user."
Name: “.$row[‘callname’].”
Email: “.$row[‘email’].”
Password: <input type=‘password’ value=’".$row[‘password’]."’ disabled=‘true’ />
Registered: “.$row[‘registered’].”
Last Login: “.$row[‘lastlogin’].”

”;
}

if($id = $_SESSION[‘id’])
{
echo "

Profile

";

$row = mysql_fetch_array($sql);
echo “






";

echo "

ID#: ”.$user."
Name: “.$row[‘callname’].”
Email: “.$row[‘email’].”
Password: <input type=‘password’ value=’".$row[‘password’]."’ disabled=‘true’ />
Registered: “.$row[‘registered’].”
Last Login: “.$row[‘lastlogin’].”


"; } }

else {
die ();
}
?>

<?php include('footer.php'); ?>[/php]

And that doesn’t work, the $check = mysql_num_rows($sql); failed because it gave a warning of:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given for that specific line

Any ideas on how I could fix this code to get it working, or how to revise my original code (posted first) to have public/private profiles?

Need to check the url, make sure that id isn’t empty. The query is failing because there’s no id.

Right, I solved that problem- made an error when setting up the tables. However I’m still getting:
Notice: Undefined index: id in myprofile.php on line 10

(I turned my error reporting on)

This is the line: $id = $_GET[‘id’];

What am I doing wrong? Doesn’t this define the ID? Upon login I grab the user’s ID in a session.

EDIT: Okay so I tried now typing in a user’s ID# (as this link: myprofile.php?id=’.$id.’ isn’t working, it’s supposed to lead them to their own profile), and it works! Sort of… it displays the ID# of the user, however the rest of the information I called from the rows (call name, email, etc) isn’t working.

You don’t need the quotes or periods in the myprofile url, just do myprofile.php?id=$id. add or die(mysql_error()); to the end of the query, see if maybe that’s failing.

Okay, added it to the query, it’s not failing as it isn’t dying:
[php]$sql = mysql_query(“SELECT * FROM users WHERE id = ‘$id’ LIMIT 1”) or die(mysql_error());[/php]

However the error is still there.

Also I set-up the link to the profile within an href code, so <a href="myprofile.php?id=$id>click</a> and it doesn’t seem to be working.

ok, well since the query is working, then maybe we just need to simplify the code abit.
[php]
// troubleshooting code
// this should tell you whether or not the id from the url was transferred.
if(!empty($_GET[‘id’])) {
echo “ID not gotten from myprofile.php”;
} else {
echo “ID Retreived”;
$id = $_GET[‘id’];
}

$sql = mysql_query(“SELECT * FROM members WHERE id = ‘$id’ LIMIT 1”);
$check = mysql_num_rows($sql);

if(mysql_num_rows($sql) == 0) {
echo “No one matches that id number!”;
} else {
while($row = mysql_fetch_array($sql)) {
$user = $id;
// rest of display code
}[/php]

First off thank you again for walking me through this, Richei. I appreciate it.

okay, here’s the updated code:
[php]<?php

session_start();

include(‘config.php’);
include(‘date.php’);

error_reporting(E_ALL);

if(!empty($_GET[‘id’])) {
echo “ID not gotten from myprofile.php”;
} else {
echo “ID Retreived”;
$id = $_GET[‘id’];
}

$sql = mysql_query(“SELECT * FROM users WHERE id = ‘$id’ LIMIT 1”);

if(mysql_num_rows($sql) == 0) {
echo “No one matches that id number!”;
} else {
while($row = mysql_fetch_array($sql)) {
$user = $id;

echo "

Profile

";

$row = mysql_fetch_array($sql);
echo “





";

echo “

ID#: ”.$user."
Name: “.$row[‘callname’].”
Email: “.$row[‘email’].”
Registered: “.$row[‘registered’].”
Last Login: “.$row[‘lastlogin’].”

”;
}

if($id = $_SESSION[‘id’])
{
echo "

Profile

";

$row = mysql_fetch_array($sql);
echo “






";

echo "

ID#: ”.$user."
Name: “.$row[‘callname’].”
Email: “.$row[‘email’].”
Password: <input type=‘password’ value=’".$row[‘password’]."’ disabled=‘true’ />
Registered: “.$row[‘registered’].”
Last Login: “.$row[‘lastlogin’].”


"; } } ?> <?php

include(‘footer.php’);

?>[/php]

when typing in the url:
http://localhost/testing/myprofile.php?id=1

it says that the id is not gotten from myprofile.php, so where should I go from here? Doesn’t the sql entry retrieve everything, even in the ID#?

It does. but the id isn’t getting to myprofile. Go back to that URL and look at the html output (view source ). Make sute tbat the URL is complete.

Here’s the view source, it shows the ID# but all the other information shows up blank. I tried entering myprofile.php?id=2 and both the public profile AND the private profile (option to edit) shows up, even though I’m not logged in as user 2, I’m logged in as user 1.

[php]Main | <a href=myprofile.php?id=$id>Profile | Inbox | Forum | To-Do List | LogoutAdmin Center9:02 pm - Sunday, April 15th

ID not gotten from myprofile.php

Profile

ID#: 1
Name:
Email:
Registered:
Last Login:

Profile

ID#: 1
Name:
Email:
Password:
Registered:
Last Login:



footer here[/php]

Then the problem seems to be with whatever page is putting that out. Looks like maybe the query isn’t working or something.

Oh wait, actually I think I spotted a typo in your process. Wouldn’t it be

// troubleshooting code
// this should tell you whether or not the id from the url was transferred.
if(empty($_GET[‘id’])) {

instead of
// troubleshooting code
// this should tell you whether or not the id from the url was transferred.
if(!empty($_GET[‘id’])) {

If so, then the ID works fine, but the rest of the information is completely blank.

So I’ve figured it out, I’m putting the $sql entry in twice, so the second time it shows up blank. So the public profiles are working, YES! However I think I’m doing something wrong php-wise with the private profile. I’m trying to make it so that if the $id = the session ID (verifying the user ID# is the same ID# profile they’re looking at), then the private profile is displayed, allowing them to edit their current information. PHP manual to see the proper term to do this and can’t find anything.

However it is showing up blank too. Will I have to redo a mysql query to get the session ID’s information? Something like
[php]$sql = mysql_query( “SELECT * FROM users WHERE id=’”.$_SESSION[‘id’]."’" ); [/php]

Anyway, thanks for taking the time to help out a newbie. Here’s the updated code:
[php]<?php

include(‘config.php’);
include(‘date.php’);

error_reporting(E_ALL);

if(empty($_GET[‘id’])) {
echo “ID not gotten from myprofile.php”;
} else {
echo “ID Retreived”;
$id = $_GET[‘id’];
}

$userfinal = $_GET[‘id’];

$user = $userfinal;

$sql = mysql_query(“SELECT * FROM users WHERE id = ‘$id’ LIMIT 1”);

if(mysql_num_rows($sql) == 0) {
echo “No one matches that id number!”;
} else {
while($row = mysql_fetch_array($sql)) {
$user = $id;

echo "

Profile

";

echo “





";

echo “

ID#: ”.$user."
Name: “.$row[‘callname’].”
Email: “.$row[‘email’].”
Registered: “.$row[‘registered’].”
Last Login: “.$row[‘lastlogin’].”

”;
}

if($id = $_SESSION[‘id’])
{
echo "

Profile

";

echo “






";

echo "

ID#: ”.$user."
Name: “.$row[‘callname’].”
Email: “.$row[‘email’].”
Password: <input type=‘password’ value=’".$row[‘password’]."’ disabled=‘true’ />
Registered: “.$row[‘registered’].”
Last Login: “.$row[‘lastlogin’].”


"; }

else {

echo “”; }
}

?>

<?php include('footer.php'); ?>[/php]

two equal signs are used for comparison, if($id == $_SESSION(‘id’]), you have 1 = sign :slight_smile: Glad you figured it out. You can take out that troubleshooting code since its picking up the id now.

Thanks! I’ve got my code working fine now, had to do another query but now it works like it should. Thanks for all the help! I have questions on how I can incorporate this in to my messaging system (ie having a users name linked to their profile) but I suppose I should start a new thread for that rather than posting here, yes? I’ve tried meddling with it, will do so for a couple days now before asking for help.

Sponsor our Newsletter | Privacy Policy | Terms of Service