Jump to content


Photo

If Statement


  • Please log in to reply
4 replies to this topic

#1 Collymore

Collymore
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 12 June 2006 - 10:49 AM

Hi.

Basically I intend a user to log on to a site (MySQL). I want to create a simple table with two fields ('user' , 'number') and depending on what number is in the number field, I want the user to be directed to a certain page.

Can this be done by an IF statement on the login page? If so, what would the IF statement look like? There could be 50 different pages so there might be a more efficent way of doing this?

For example, if the number in the field is 22, then they will go to 22.php to interact with a table called 22.

Thanks in advance.



#2 d_barszczak

d_barszczak
  • Members
  • PipPipPip
  • Advanced Member
  • 188 posts
  • LocationBradford - UK

Posted 12 June 2006 - 11:47 AM

[!--quoteo(post=382791:date=Jun 12 2006, 11:49 AM:name=Collymore)--][div class=\'quotetop\']QUOTE(Collymore @ Jun 12 2006, 11:49 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hi.

Basically I intend a user to log on to a site (MySQL). I want to create a simple table with two fields ('user' , 'number') and depending on what number is in the number field, I want the user to be directed to a certain page.

Can this be done by an IF statement on the login page? If so, what would the IF statement look like? There could be 50 different pages so there might be a more efficent way of doing this?

For example, if the number in the field is 22, then they will go to 22.php to interact with a table called 22.

Thanks in advance.
[/quote]

There may be more efficent ways of doing this but it really depends on the data they will be viewing once they have logged in.

If i was trying the above i would have a table with 3 columns ie username password page in a table called users and have the form submitted to something like this.


<?php

$user = "username";
$pass = "password";
$host = "localhost";

$data = "database";

$query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";

mysql_connect($host, $user, $pass) or die(mysql_error());

mysql_select_db($data);

$report = mysql_query($query);

$num = mysql_num_rows($report);

if ($num == 1) {
     $row = mysql_fetch_array($report, MYSQL_ASSOC);
     extract($row);
         echo "<script language='javascript' type='text/javascript'>window.location.replace('path/page.php')</script>";
} else {
// Fail Script goes here...
}
?>

When all else fails - Try reading the manual.

Integrated IT Systems Ltd


#3 Collymore

Collymore
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 12 June 2006 - 01:17 PM

[!--quoteo(post=382795:date=Jun 12 2006, 06:47 AM:name=scripts2go.co.uk)--][div class=\'quotetop\']QUOTE(scripts2go.co.uk @ Jun 12 2006, 06:47 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
There may be more efficent ways of doing this but it really depends on the data they will be viewing once they have logged in.

If i was trying the above i would have a table with 3 columns ie username password page in a table called users and have the form submitted to something like this.
<?php

$user = "username";

$pass = "password";
$host = "localhost";

$data = "database";

$query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";

mysql_connect($host, $user, $pass) or die(mysql_error());

mysql_select_db($data);

$report = mysql_query($query);

$num = mysql_num_rows($report);

if ($num == 1) {
     $row = mysql_fetch_array($report, MYSQL_ASSOC);
     extract($row);
         echo "<script language='javascript' type='text/javascript'>window.location.replace('path/page.php')</script>";
} else {
// Fail Script goes here...
}
?>
[/quote]


Thanks for the reply. They will not be viewing any data out of the tables but just running a script to update their records. As there is a progressive theme to the site, I need the site to know what stage the user is at.

Example.


A child is attempting to swim 5 metres, once he has completed 5 metres, the next week he will need to attempt a new target inputted by the parent. The teacher can then input if the target has been reached...and so on
The history of this needs to be recorded so that’s why I’m using more than one table rather than just deleting and replacing data out of one table.

I'm sure all this could be done with just 1 script rather than the dozens I'll have! but im afraid my PHP knowledge is quite poor! My main problem seems to be the site automatically knowing which week it needs to enter the data into with out asking the user.

I don't understand from your code how the 'page' field is manipulated?

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 12 June 2006 - 01:47 PM

[!--quoteo(post=382791:date=Jun 12 2006, 05:49 AM:name=Collymore)--][div class=\'quotetop\']QUOTE(Collymore @ Jun 12 2006, 05:49 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hi.

Basically I intend a user to log on to a site (MySQL). I want to create a simple table with two fields ('user' , 'number') and depending on what number is in the number field, I want the user to be directed to a certain page.

Can this be done by an IF statement on the login page? If so, what would the IF statement look like? There could be 50 different pages so there might be a more efficent way of doing this?

For example, if the number in the field is 22, then they will go to 22.php to interact with a table called 22.

Thanks in advance.
[/quote]

login.php
<?php
session_start();

if ($_POST['login']) {
   if (!$_POST['user'] || !$_POST['pass']) {
      echo "no username or password entered.";
   } else {
      $conn = mysql_connect('localhost','username','password');
      $db = mysql_select_db('dbname',$conn);

      $user = mysql_real_escape_string($_POST['user']);
      $pass = mysql_real_escape_string($_POST['pass']);
 
      $sql = "select * from tablename where user = '$user' and pass = '$pass'";
      $result = mysql_query($sql);

      $is_user = mysql_num_rows($result);
      if ($is_user > 0) {
         $info = mysql_fetch_array($result);
         $_SESSION['userinfo'] = $info;
         header("location: targetpage".$info['page'].".php");
         exit();
      } else {
        echo "incorrect username or password.";
      } 
   }
} else {
   $form = "<form action='{$_SERVER['PHP_SELF']}' method = 'post'>";
   $form.= "Name:<input type='text' name = 'user'><br>";
   $form.= "Password:<input type='password' name='pass'><br>";
   $form.= "<input type='submit' name = 'login' value='login'>";

   echo $form;
}
?>

targetpage1.php (or whatever)
<?php
session_start();
if ($_SESSION['userinfo']) {
  $userinfo = $_SESSION['userinfo']);
} else {
   header("Location: login.php");
   exit();
}   

echo "hello ". $userinfo['user'] . " you are logged in and on page " . $userinfo['page'];

?>

there are much more efficient ways to do your scripting though. for instance, if you have 50 pages that are all going to do about the same thing, you would want to have 1 page that just checks for which number to base the script off of, and you would do something like this:

header("Location: targetpage.php?page=".$info['page']);

which will send you to

targetpage.php?page=2

for example. and then you would check which page it is by something like so:

if ($_GET['page']) {
$page = $_GET['page'];
}

and then base your code on $page
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 Collymore

Collymore
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 12 June 2006 - 02:05 PM

[!--quoteo(post=382825:date=Jun 12 2006, 08:47 AM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ Jun 12 2006, 08:47 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
login.php
<?php
session_start();

if ($_POST['login']) {
   if (!$_POST['user'] || !$_POST['pass']) {
      echo "no username or password entered.";
   } else {
      $conn = mysql_connect('localhost','username','password');
      $db = mysql_select_db('dbname',$conn);

      $user = mysql_real_escape_string($_POST['user']);
      $pass = mysql_real_escape_string($_POST['pass']);
 
      $sql = "select * from tablename where user = '$user' and pass = '$pass'";
      $result = mysql_query($sql);

      $is_user = mysql_num_rows($result);
      if ($is_user > 0) {
         $info = mysql_fetch_array($result);
         $_SESSION['userinfo'] = $info;
         header("location: targetpage".$info['page'].".php");
         exit();
      } else {
        echo "incorrect username or password.";
      } 
   }
} else {
   $form = "<form action='{$_SERVER['PHP_SELF']}' method = 'post'>";
   $form.= "Name:<input type='text' name = 'user'><br>";
   $form.= "Password:<input type='password' name='pass'><br>";
   $form.= "<input type='submit' name = 'login' value='login'>";

   echo $form;
}
?>

targetpage1.php (or whatever)
<?php
session_start();
if ($_SESSION['userinfo']) {
  $userinfo = $_SESSION['userinfo']);
} else {
   header("Location: login.php");
   exit();
}   

echo "hello ". $userinfo['user'] . " you are logged in and on page " . $userinfo['page'];

?>

there are much more efficient ways to do your scripting though. for instance, if you have 50 pages that are all going to do about the same thing, you would want to have 1 page that just checks for which number to base the script off of, and you would do something like this:

header("Location: targetpage.php?page=".$info['page']);

which will send you to

targetpage.php?page=2

for example. and then you would check which page it is by something like so:

if ($_GET['page']) {
$page = $_GET['page'];
}

and then base your code on $page
[/quote]

Thanks for the detail. I will give it a shot.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users