Page 1 of 1

View Profile Problem.

Posted: Tue Mar 30, 2004 9:53 am
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

Posted: Tue Mar 30, 2004 10:23 am
by Steveo31
Well, what is the error?

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

:)

Posted: Tue Mar 30, 2004 10:49 am
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'";

Posted: Tue Mar 30, 2004 11:19 am
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.

Posted: Tue Mar 30, 2004 11:36 am
by Joe
OK ill do that now. Thanks

Posted: Tue Mar 30, 2004 11:56 am
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

Posted: Tue Mar 30, 2004 12:04 pm
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>";

Posted: Tue Mar 30, 2004 2:30 pm
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

Posted: Tue Mar 30, 2004 2:32 pm
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

Posted: Tue Mar 30, 2004 2:41 pm
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?

Posted: Tue Mar 30, 2004 3:20 pm
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))

Posted: Tue Mar 30, 2004 3:40 pm
by delorian
This is what happens when you do not use

Code: Select all

when posting your code

Posted: Tue Mar 30, 2004 4:30 pm
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)