Jump to content

Recommended Posts

<?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

 

Link to comment
https://forums.phpfreaks.com/topic/52291-solved-inserting-an-if-statement/
Share on other sites

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. :)

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");
?>

<?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();
}
?>

<?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"];
    }
  }

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.