如何从我的数据库中选择数据并在下一页中将其用作变量?

时间:2015-08-07 10:43:12

标签: php mysql

我想从MySQL数据库中选择数据,并在下一页中将该数据用作变量。

换句话说,当用户登录(signin_up.php)并且用户正在验证(checklogin.php)时,如何获取用户在注册时输入的区域名称(region_name),该名称存储在数据库中(user_signup)并将其用于下一页的查询(regional_head_page.php)。

这是我的相应网页代码:

signin_up.php

      <form class="navbar-form navbar-right"  method='post' action="checklogin.php">
        <div class="form-group">
          <label for="inputEmail" class="sr-only">Email address</label>
          <input name="eemail" type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>            
        </div>
        <div class="form-group">
          <label for="inputPassword" class="sr-only">Password</label>
          <input name="district_pass" type="password" id="inputPassword" class="form-control" placeholder="Password" required>            
        </div>
        <button name="signin" type="submit" align="right" class="btn btn-success">Sign in</button>
      </form>

checklogin.php

<?php
ob_start();
session_start();


$email = $_POST['eemail'];
$password = $_POST['district_pass'];
$region_name = 'region_name';


$conn = mysqli_connect('localhost', 'root', 'myspace4199', 'car_sales');

$email = mysqli_real_escape_string($conn, $email);

$query = "SELECT *
FROM user_signup
WHERE eemail = '$email';";

$result = mysqli_query($conn, $query);

if(mysqli_num_rows($result) == 0) // User not found. So, redirect to login_form again.
{
    ?>
        <script type="text/javascript">
            alert("The email address or password is invalid.");
            history.back();
        </script>
    <?php
}

$userData = mysqli_fetch_array($result, MYSQL_ASSOC);



//Check to see if user is an employee or just a member
$generaluserCheck = mysqli_query($conn, "SELECT * FROM user_signup WHERE (eemail = '$email') AND (access_level = 'general_user')");
$ministryheadCheck = mysqli_query($conn, "SELECT * FROM user_signup WHERE (eemail = '$email') AND (access_level = 'ministry_head')");
$regionalheadCheck = mysqli_query($conn, "SELECT * FROM user_signup WHERE (eemail = '$email') AND (access_level = 'regional_head')");
$carcompanyCheck = mysqli_query($conn, "SELECT * FROM user_signup WHERE (eemail = '$email') AND (access_level = 'car_company')");
$adminCheck = mysqli_query($conn, "SELECT * FROM user_signup WHERE (eemail = '$email') AND (access_level = 'admin')");

if($password == $userData['district_pass'] && mysqli_num_rows($generaluserCheck) == 1) // Incorrect password. So, redirect to login_form again.
{
    header('Location: homepage.php');
}

else if($password == $userData['district_pass'] && mysqli_num_rows($ministryheadCheck) == 1){
    header('Location: ministry_head_page.php');
}
else if($password == $userData['district_pass'] && mysqli_num_rows($regionalheadCheck) == 1){
    //header('Location: regional_head_page.php');
     header('Location: regional_head_page.php?region_name='.$region_name);
}
else if($password == $userData['district_pass'] && mysqli_num_rows($carcompanyCheck) == 1){
    header('Location: car_company_page.php');
}   
else if($password == $userData['district_pass'] && mysqli_num_rows($adminCheck) == 1){
    header('Location: adminHome.php');
}
else {
        ?>
        <script type="text/javascript">
            alert("The email address or password is invalid.");
            history.back();
        </script>
    <?php
}

?>

regional_head_page.php

<?php 
session_start();
$region_name = 'region_name';
$con=mysqli_connect("localhost","root","myspace4199","car_sales");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="SELECT * FROM general_reservation WHERE region_name = '$region_name'";

$records=mysqli_query($con,$sql);


?>

2 个答案:

答案 0 :(得分:0)

checklogin.php

$_SESSION['region_name'] = $region_name = 'region_name'; // Assign to session variable.

然后,在 regional_head_page.php

$region_name = $_SESSION['region_name']; // Retrieve from session variable.

答案 1 :(得分:0)

在checklogin.php下

<?php 
session_start();
$region_name = $_SESSION['region_name']; // Retrieve from session variable.
$con=mysqli_connect("localhost","root","myspace4199","car_sales");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if ( isset($_SESSION['region_name']) ) {
     $region_name = $_SESSION['region_name'];
 } else {
     // got a problem, deal with it here
 }

$sql="SELECT * FROM general_reservation";

$records=mysqli_query($con,$sql);


?>

在regional_head_page.php下

argv[1]