hoponhiggo Posted July 19, 2011 Share Posted July 19, 2011 Hello I am using the code below, which is fine, but in the part of the code that follows the 'while' statement near the bottom, i want to use the $img variable for a second time in between the <div id='mempic'> tags, to display a picture but its not working. Can anybody tel me why? <?php echo "<center>"; if(isset($_GET['user'])) { //if there trying to view a profile //gets the user name and makes it safe $username = $_GET[user]; //querys the db to find the username $getuser = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'"); //checks see if the username exists in the db $usernum = mysql_num_rows($getuser); //if it don't exist if($usernum == 0) { //don't exist echo ("User Not Found"); } //if it does exist then show there profile else{ $user = mysql_fetch_array($getuser); //to display image from source $dir = "prof_pics"; $sql = "SELECT prof_pic FROM users WHERE username = '$username'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0) die("Username not found in database."); $row = mysql_fetch_array($res); $pic="$dir/".$row['prof_pic']; $img="<img src=\"$pic\" width=\"88\" height=\"88\" align=\"center\"><br>"; echo " <b>$user[username]'s Profile</b><br><br> $img <br> Email: $user[email]<br> <a href='friendrequest.php?user=$user[username]'>Add as Friend</a> "; } }else{ //gets all the members from the database $getusers = mysql_query("SELECT * FROM `users` ORDER BY `uid` ASC") or die(mysql_error()); //loops there name out while ( $user = mysql_fetch_array($getusers)) { echo "<div id='memcontainer'> <div id='mempic'>$img</div> <div id='memname'><a href='members.php?user=$user[username]'>$user[username]</a></div></div><br> "; } } echo "<center>"; ?> The first instance of the $img works fine, but not the second one? Link to comment https://forums.phpfreaks.com/topic/242355-why-wont-img-work/ Share on other sites More sharing options...
djlee Posted July 19, 2011 Share Posted July 19, 2011 compare the code, your defining $img in the first one but not the second block of code for output. Link to comment https://forums.phpfreaks.com/topic/242355-why-wont-img-work/#findComment-1244748 Share on other sites More sharing options...
AyKay47 Posted July 19, 2011 Share Posted July 19, 2011 you defined the $img variable inside of the if else statement, and only there can it be called without redefining it Link to comment https://forums.phpfreaks.com/topic/242355-why-wont-img-work/#findComment-1244750 Share on other sites More sharing options...
Zane Posted July 19, 2011 Share Posted July 19, 2011 $img probably doesn't work because you only create it at a condition. you should create the $img before any if statements if you want to use it multiple times. And I'm sure there's another easier fix for it, but I really don't want to look through all that code. You should post snippets of what you're talking about below all that. Link to comment https://forums.phpfreaks.com/topic/242355-why-wont-img-work/#findComment-1244751 Share on other sites More sharing options...
AdRock Posted July 19, 2011 Share Posted July 19, 2011 you defined the $img variable inside of the if else statement, and only there can it be called without redefining it I was just about to say that. The image hasn't been called in the first place if the else is taken. Link to comment https://forums.phpfreaks.com/topic/242355-why-wont-img-work/#findComment-1244752 Share on other sites More sharing options...
hoponhiggo Posted July 19, 2011 Author Share Posted July 19, 2011 ah right. didnt realise i had to redefine the variable. Does that mean i would also have to redefine the $username variable too? //to display image from source again $dir = "prof_pics"; $sql = "SELECT prof_pic FROM users WHERE username = '$username'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0) die("Username not found in database."); $row = mysql_fetch_array($res); $pic="$dir/".$row['prof_pic']; $img="<img src=\"$pic\" width=\"88\" height=\"88\" align=\"center\"><br>"; Link to comment https://forums.phpfreaks.com/topic/242355-why-wont-img-work/#findComment-1244781 Share on other sites More sharing options...
AyKay47 Posted July 19, 2011 Share Posted July 19, 2011 yes, but I would do that before the condition anyway. $username = $_GET['user']; if(isset($username)) { this way you won't have to redefine Link to comment https://forums.phpfreaks.com/topic/242355-why-wont-img-work/#findComment-1244800 Share on other sites More sharing options...
hoponhiggo Posted July 19, 2011 Author Share Posted July 19, 2011 $username = $_GET['user']; if(isset($username)) { i cant get this to work. based on my code above, where exactly would you put this? Link to comment https://forums.phpfreaks.com/topic/242355-why-wont-img-work/#findComment-1244830 Share on other sites More sharing options...
AyKay47 Posted July 19, 2011 Share Posted July 19, 2011 <?php echo "<center>"; $username = $_GET['user']; //very top if(isset($username) { //if there trying to view a profile //gets the user name and makes it safe //querys the db to find the username $getuser = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'"); //checks see if the username exists in the db $usernum = mysql_num_rows($getuser); //if it don't exist if($usernum == 0) { //don't exist echo ("User Not Found"); } //if it does exist then show there profile else{ $user = mysql_fetch_array($getuser); //to display image from source $dir = "prof_pics"; $sql = "SELECT prof_pic FROM users WHERE username = '$username'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0) die("Username not found in database."); $row = mysql_fetch_array($res); $pic="$dir/".$row['prof_pic']; $img="<img src=\"$pic\" width=\"88\" height=\"88\" align=\"center\"><br>"; echo " <b>$user[username]'s Profile</b><br><br> $img <br> Email: $user[email]<br> <a href='friendrequest.php?user=$user[username]'>Add as Friend</a> "; } }else{ //gets all the members from the database $getusers = mysql_query("SELECT * FROM `users` ORDER BY `uid` ASC") or die(mysql_error()); //loops there name out while ( $user = mysql_fetch_array($getusers)) { echo "<div id='memcontainer'> <div id='mempic'>$img</div> <div id='memname'><a href='members.php?user=$user[username]'>$user[username]</a></div></div><br> "; } } echo "<center>"; ?> Link to comment https://forums.phpfreaks.com/topic/242355-why-wont-img-work/#findComment-1244832 Share on other sites More sharing options...
hoponhiggo Posted July 20, 2011 Author Share Posted July 20, 2011 Sorry, still getting my 'username not found in database' message. I believe the problem is to do with my $username in the mysql query? Full code below if anybody can help? <?php echo "<center>"; $username = $_GET[user]; //gets the user name and makes it safe if(isset($_GET['user'])) { //if there trying to view a profile //querys the db to find the username $getuser = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'"); //checks see if the username exists in the db $usernum = mysql_num_rows($getuser); //if it don't exist if($usernum == 0) { //don't exist echo ("User Not Found"); } //if it does exist then show there profile else{ $user = mysql_fetch_array($getuser); //to display image from source $dir = "prof_pics"; $sql = "SELECT prof_pic FROM users Where username = '$username'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0) die("Username not found in database."); $row = mysql_fetch_array($res); $pic="$dir/".$row['prof_pic']; $img="<img src=\"$pic\" width=\"88\" height=\"88\" align=\"center\"><br>"; echo " <b>$user[username]'s Profile</b><br><br> $img <br> Email: $user[email]<br> <a href='friendrequest.php?user=$user[username]'>Add as Friend</a> "; } }else{ //gets all the members from the database $getusers = mysql_query("SELECT * FROM `users` ORDER BY `uid` ASC") or die(mysql_error()); //to display image from source again $dir = "prof_pics"; $sql = "SELECT prof_pic FROM users Where username = '$username'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0) die("Username not found in database."); $row = mysql_fetch_array($res); $pic="$dir/".$row['prof_pic']; $img="<img src=\"$pic\" width=\"88\" height=\"88\" align=\"center\"><br>"; //loops there name out while ( $user = mysql_fetch_array($getusers) ) { echo "<table><td><div id='memcontainer'> <div id='mempic'>$img</div> <div id='memname'><a href='members.php?user=$user[username]'>$user[username]</a></div></div><br></td> </table> "; } } echo "<center>"; ?> Link to comment https://forums.phpfreaks.com/topic/242355-why-wont-img-work/#findComment-1245033 Share on other sites More sharing options...
hoponhiggo Posted July 20, 2011 Author Share Posted July 20, 2011 I have changed the later part of the code to: else{ //gets all the members from the database $getusers = mysql_query("SELECT * FROM `users` ORDER BY `uid` ASC") or die(mysql_error()); //loops there name out while ( $user = mysql_fetch_array($getusers) ) $username = $user[username]; //to display image from source again $dir = "prof_pics"; $sql = "SELECT prof_pic FROM users Where username = '$username'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0) die("Username not found in database."); $row = mysql_fetch_array($res); $pic="$dir/".$row['prof_pic']; $img="<img src=\"$pic\" width=\"88\" height=\"88\" align=\"center\"><br>"; { echo "<table><td><div id='memcontainer'> <div id='mempic'>$img</div> <div id='memname'><a href='members.php?user=$user[username]'>$user[username]</a></div></div><br></td> </table> "; Which is now only showing one record, and other info such as $user is missing? Link to comment https://forums.phpfreaks.com/topic/242355-why-wont-img-work/#findComment-1245098 Share on other sites More sharing options...
AyKay47 Posted July 20, 2011 Share Posted July 20, 2011 you need to be wrapping your indices in quotes.. $username = $_GET[user]; should be $username = $_GET['user']; //$_GET is associative and the index needs to be wrapped in quotes unless its an integer Link to comment https://forums.phpfreaks.com/topic/242355-why-wont-img-work/#findComment-1245109 Share on other sites More sharing options...
hoponhiggo Posted July 20, 2011 Author Share Posted July 20, 2011 Ok, wrapped them up. Still no change. Any other ideas? Link to comment https://forums.phpfreaks.com/topic/242355-why-wont-img-work/#findComment-1245135 Share on other sites More sharing options...
dcro2 Posted July 20, 2011 Share Posted July 20, 2011 You're missing some braces to start and end the while() so only this line is being executed for each row: $username = $user[username]; I can't figure out why this brace is here either: $img="<img src=\"$pic\" width=\"88\" height=\"88\" align=\"center\"><br>"; { echo "<table><td><div id='memcontainer'> Link to comment https://forums.phpfreaks.com/topic/242355-why-wont-img-work/#findComment-1245138 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.