textbox Posted May 21, 2007 Share Posted May 21, 2007 <?php include "global/db.php"; $username = $_GET["username"]; if (!preg_match('/^[\w\d\s\-_]+$/', $username)) die('wrong username'); $q = mysql_query("SELECT * FROM users WHERE username = '$username'"); $are = mysql_fetch_assoc($q); //echo "Welcome to ".$username."'s profile page."; $sql = "SELECT * FROM users WHERE username = '$username'"; $result = mysql_query($sql) OR die(mysql_error()); $num=mysql_num_rows($result); $about=mysql_result($result,0,"about"); $interests=mysql_result($result,0,"interests"); $music=mysql_result($result,0,"music"); $film=mysql_result($result,0,"film"); $quote=mysql_result($result,0,"quote"); $headline=mysql_result($result,0,"headline"); ?> How, and where would i include an IF statement that would redirect to another page if the username has not been found. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/52291-solved-inserting-an-if-statement/ Share on other sites More sharing options...
ryeman98 Posted May 21, 2007 Share Posted May 21, 2007 I'm not entirely sure but I think it would be like this: *Note - I haven't tested this and I'm not sure if it will work but I'll try. <?php include "global/db.php"; $username = $_GET["username"]; if (!preg_match('/^[\w\d\s\-_]+$/', $username)) die("Location: index.php"); $q = mysql_query("SELECT * FROM users WHERE username = '$username'"); $are = mysql_fetch_assoc($q); //echo "Welcome to ".$username."'s profile page."; $sql = "SELECT * FROM users WHERE username = '$username'"; $result = mysql_query($sql) OR die(mysql_error()); $num=mysql_num_rows($result); $about=mysql_result($result,0,"about"); $interests=mysql_result($result,0,"interests"); $music=mysql_result($result,0,"music"); $film=mysql_result($result,0,"film"); $quote=mysql_result($result,0,"quote"); $headline=mysql_result($result,0,"headline"); ?> Again, I have no idea if this will work at all but hey, I'm tryin. Quote Link to comment https://forums.phpfreaks.com/topic/52291-solved-inserting-an-if-statement/#findComment-257975 Share on other sites More sharing options...
textbox Posted May 21, 2007 Author Share Posted May 21, 2007 Sadly, it did not. Ive tried that already thinking 'yeah, this work' Ah well, thanks for having a go anyway!! Quote Link to comment https://forums.phpfreaks.com/topic/52291-solved-inserting-an-if-statement/#findComment-257979 Share on other sites More sharing options...
ryeman98 Posted May 21, 2007 Share Posted May 21, 2007 No problem. At least I didn't make a fool of myself Quote Link to comment https://forums.phpfreaks.com/topic/52291-solved-inserting-an-if-statement/#findComment-257983 Share on other sites More sharing options...
OOP Posted May 21, 2007 Share Posted May 21, 2007 Hi there, it will be somthing like this <?php include "global/db.php"; $username = $_GET["username"]; if(empty($username)){ header("Location: http://www.example.com/"); exit(); } if (!preg_match('/^[\w\d\s\-_]+$/', $username)) die("Location: index.php"); $q = mysql_query("SELECT * FROM users WHERE username = '$username'"); $are = mysql_fetch_assoc($q); //echo "Welcome to ".$username."'s profile page."; $sql = "SELECT * FROM users WHERE username = '$username'"; $result = mysql_query($sql) OR die(mysql_error()); $num=mysql_num_rows($result); $about=mysql_result($result,0,"about"); $interests=mysql_result($result,0,"interests"); $music=mysql_result($result,0,"music"); $film=mysql_result($result,0,"film"); $quote=mysql_result($result,0,"quote"); $headline=mysql_result($result,0,"headline"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52291-solved-inserting-an-if-statement/#findComment-257987 Share on other sites More sharing options...
textbox Posted May 21, 2007 Author Share Posted May 21, 2007 Hello, no luck on that front either! I wonder if the original code is wrong?! Quote Link to comment https://forums.phpfreaks.com/topic/52291-solved-inserting-an-if-statement/#findComment-258199 Share on other sites More sharing options...
textbox Posted May 21, 2007 Author Share Posted May 21, 2007 Am i right in trying to do it here if(empty($username)){ header("Location: http://www.example.com/"); exit(); } Or do i need to do something like check how many results are returned, if its 0, forward to my error page?! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/52291-solved-inserting-an-if-statement/#findComment-258311 Share on other sites More sharing options...
JakeTheSnake3.0 Posted May 21, 2007 Share Posted May 21, 2007 <?php include "global/db.php"; if (isset($_GET["username"])) { $username = $_GET["username"]; if (!preg_match('/^[\w\d\s\-_]+$/', $username)) die("Location: index.php"); $q = mysql_query("SELECT * FROM users WHERE username = '$username'"); $are = mysql_fetch_assoc($q); //echo "Welcome to ".$username."'s profile page."; $sql = "SELECT * FROM users WHERE username = '$username'"; $result = mysql_query($sql) OR die(mysql_error()); $num=mysql_num_rows($result); $about=mysql_result($result,0,"about"); $interests=mysql_result($result,0,"interests"); $music=mysql_result($result,0,"music"); $film=mysql_result($result,0,"film"); $quote=mysql_result($result,0,"quote"); $headline=mysql_result($result,0,"headline"); } else { header("Location: http://www.example.com/"); exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52291-solved-inserting-an-if-statement/#findComment-258346 Share on other sites More sharing options...
trq Posted May 21, 2007 Share Posted May 21, 2007 <?php include "global/db.php"; $username = $_GET["username"]; if ($result = mysql_query("SELECT * FROM users WHERE username = '$username'")) { if (!mysql_num_rows($result)) { // username not found, do redirect. header("Location: usernotfound.php"); } else { $row = mysql_fetch_assoc($result); //echo "Welcome to ".$username."'s profile page."; $about = $row["about"]; $interests = $row["interests"]; $music = $row["music"]; $film = $row["film"]; $quote = $row["quote"]; $headline = $row["headline"]; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52291-solved-inserting-an-if-statement/#findComment-258359 Share on other sites More sharing options...
textbox Posted May 21, 2007 Author Share Posted May 21, 2007 Thanks Thorpe. Sorted Quote Link to comment https://forums.phpfreaks.com/topic/52291-solved-inserting-an-if-statement/#findComment-258365 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.