View Profile Problem.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

View Profile Problem.

Post by Joe »

Recently i have been trying to create a script where you can click on a members name and view their profile. The best way i could think of is making a table column named 'userprofile' and insert a link tag to their profile ID. My code so far go's like:

$link = mysql_connect("???", "???", "???") or die("Could not connect");
mysql_select_db("???");
$query = "SELECT * FROM members WHERE username = '$username'";
$result = mysql_query($query) or die("Error");
$row = mysql_fetch_assoc($result);

if (mysql_num_rows($result))
{
$row = mysql_fetch_assoc($result);
$userprofile1 = "<A href='http://www.mysite.com/viewprofile?ID=".$row['ID']."'>".$username."</A>";
$query = "UPDATE members SET userprofile = '$userprofile1'";
$result = mysql_query($query) or die("Query Error!");


Obviously i have created variable values for the names ect... But i keep getting a query error. Can someone please help me out here!!!

Regards


Joe - God is good, I am better
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Well, what is the error?

May I suggest using "... or die(mysql_error());" as it gives the error as MySQL sees it.

:)
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

I get an error saying, Query Error!. Which most obviously means that i have a problem with the query:

$userprofile1 = "<A href='http://www.mysite.com/viewprofile?ID=".$row['ID']."'>".$username."</A>";
$query = "UPDATE members SET userprofile = '$userprofile1'";
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

try echoing out $query and making sure it is correct. Also do as Steveo suggest and use mysql_error() to see the exact error.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

OK ill do that now. Thanks
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

I tried echoing variables with no luck at all. It shows the username as its stored in the code itself but it was not attaching the ID number to the end of the link. The code go's like:

<?php
$username = "Joe"; //Test varaibe. The value will actually be passed from a form link.
$link = mysql_connect("???", "???", "???") or die("Could not connect");
mysql_select_db("???");
$query = "SELECT * FROM members WHERE username = '$username'";
$result = mysql_query($query) or die("Error");
$row = mysql_fetch_assoc($result);

if (mysql_num_rows($result))
{
$row = mysql_fetch_array($result);
$ID1 = $row['ID'];
$userprofile1 = "<A href='http://www.mysite.com/viewprofile?ID=$I ... rname."</A>";
echo $userprofile1;
mysql_close($link);
}

I am now going to try Steveo's method on the real code. Any more help would be great as im struggling deeply with this?

regards


Joe 8) God is good. I am better
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

Make a var_dump after:

$row = mysql_fetch_array($result);

echo "<pre>";
var_dump($row);
echo "</pre>";

if the $row['ID"] is ok,
try this:
$userprofile1 = "<a href='http://www.mysite.com/viewprofile?ID=". ... rname."</A>";
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

I tried that aswell delorian with no luck. I yet again done another small test but this time change the value of $userprofile to 1 instead of the ID column. The code for that test went like:

<?php
$username = "Mcaster";
$link = mysql_connect("???", "???", "???") or die("Could not connect");
mysql_select_db("???");
$query = "SELECT * FROM members WHERE username = '$username'";
$result = mysql_query($query) or die("Error");

if (mysql_num_rows($result))
{
$row = mysql_fetch_array($result);
$ID1 = "1";
$userprofile1 = "<a href='http://www.mysite.com/viewprofile?ID=". ... rname."</A>";
echo $userprofile1;
}
?>

And all worked well, giving me the data for member ID #1 but i just cant seem to get it to add the $row['ID'] instead of assigning the variable with a direct value. Any help!!!


Regards



Joe God is good. I am better
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

I tried that aswell delorian with no luck. I yet again done another small test but this time change the value of $userprofile to 1 instead of the ID column. The code for that test went like:

<?php
$username = "Joe";
$link = mysql_connect("???", "???", "???") or die("Could not connect");
mysql_select_db("???");
$query = "SELECT * FROM members WHERE username = '$username'";
$result = mysql_query($query) or die("Error");

if (mysql_num_rows($result))
{
$row = mysql_fetch_array($result);
$ID1 = "1";
$userprofile1 = "<a href='http://www.mysite.com/viewprofile?ID=". ... rname."</A>";
echo $userprofile1;
}
?>

And all worked well, giving me the data for member ID #1 but i just cant seem to get it to add the $row['ID'] instead of assigning the variable with a direct value. Any help!!!


Regards



Joe God is good. I am better
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Try replacing....

Code: Select all

$userprofile1 = "<a href='http://www.mysite.com/viewprofile?ID=".$ID1."'>".$username."</A>";
with....

Code: Select all

$userprofile1 = "<a href='http://www.mysite.com/viewprofile?ID=".$ID1."'>".$username."</A>"; 
$userprofile1 = addslashes($userprofile1);
I also notice that you do not have a WHERE clause in your update statement therfore all rows will be updated?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

$query = "SELECT * FROM members WHERE username = '$username'";
$result = mysql_query($query) or die("Error");
$row = mysql_fetch_assoc($result);

if (mysql_num_rows($result))
{
$row = mysql_fetch_array($result);
$ID1 = $row['ID'];
$userprofile1 = "<A href='http://www.mysite.com/viewprofile?ID=$I ... rname."</A>";
echo $userprofile1;
mysql_close($link);
}
If there's one row returned, then you are first doing a mysql_fetch_assoc(), this grabs the row, then you try a mysql_fetch_array() which will be empty, as you've already got the row. So you basically need to remove the first mysql_fetch_assoc() and leave the fetch inside the if(mysql_num_rows($result))
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

This is what happens when you do not use

Code: Select all

when posting your code
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

delorian wrote:This is what happens when you do not use

Code: Select all

when posting your code  [/quote]
using the
tags would not have affected that. They wouldn't have pointed that out to you.

Code: Select all

<?php
$query = "SELECT * FROM members WHERE username = '$username'";
$result = mysql_query($query) or die("Error");
$row = mysql_fetch_assoc($result);

if (mysql_num_rows($result))
{
$row = mysql_fetch_array($result);
$ID1 = $row['ID'];
$userprofile1 = "<A href='http://www.mysite.com/viewprofile?ID=$ID1'>".$username."</A>";
echo $userprofile1;
mysql_close($link);
}
?>
EDIT: why are my php tags messed up here?? - Admin Edit: it was the

Code: Select all

in the quote from delorian, should be ok now  (weird bug though)
Post Reply