【问题标题】:SELECT from MySQL Database with PHP code使用 PHP 代码从 MySQL 数据库中选择
【发布时间】:2013-04-10 11:28:51
【问题描述】:

现在我的登录页面工作正常。如果用户名和密码都正确,则返回值 1 并将您定向到名为 studentloggedin.php 的页面 在我的数据库表中,我有一个名为“职业”的字段,其中有两个值:“学生”或“讲师”。

我的问题是创建一个 if 语句,如果用户是学生,它会将我定向到 studentloggedin.php,否则它会将您定向到讲师loggedin.php

我已经尝试进行查询,但它不起作用..请帮助 :) 我的代码在这里:

<?php 
session_start();
//*********Server Information to establish a connection ******

$host        =    'localhost';
$user        =    'kurtfarrugia';
$password    =    '1234';
$database    =    'kurt_farrugia';

$link = mysql_connect($host,$user,$password) or die('Error in Server information');
mysql_select_db($database,$link) or die('Can not Select Databasse');

//***************End Connection Establishment***************************************
//*******Form Information********

$userName = mysql_real_escape_string($_POST['username']); //User Name sent from Form
$password = mysql_real_escape_string($_POST['password']); // Password sent from Form

$rememberMe = strip_tags($_POST['rememberMe']);
setcookie("username", $_POST['username']);

//*********retrieving data from Database**********

$query = "select * from tbladmin where admin_usr_name='$userName' and          
admin_pwd='$password'";

$res = mysql_query($query);

$rows = mysql_num_rows($res);

//**********it ensures that the script does not continue unless the username varaible     is not null and not empty
if (!empty($_POST['username'])) {
    $username = mysql_real_escape_string(strip_tags(trim($_POST['username'])));
} else {
    //die('Hey turd, go back and fill in your username!');
    header( "Location: login_fail2.php" ); die;
}

/*if ($rememberMe) {
    setcookie("loggedIn", "yes", time()+3600);

    header ("Location: studentloggedin.php" ); die;
             }
else {
     echo "Username and/or password is incorrect.";
}


if ($_COOKIE['loggedIn'] == "yes") {
    header ("Location: studentloggedin.php" );
    die();
}
*/
// HERE is the query 

//$result2 = mysqli_query($con,"SELECT occupation FROM tbladmin WHERE admin_usr_name    ='$username'" == 'student');
//$row == mysqli_fetch_array($result)

//**********if $userName and $password will match database, The above function will    return 1 row
if($rows==1)
//{
{
 //***if the userName and password matches then register a session and redrect user to    the studentloggedin.php

 //$_SESSION['userName'];
 //$_SESSION['password'];

    header("location:studentloggedin.php");
}
//}

else
{
    header( "Location: login_fail2.php" ); die;
}

?> 

【问题讨论】:

  • 请停止使用mysql_*函数..切换到PDO或mysqli_*
  • 尝试将 header("location:studentloggedin.php"); 更改为 header("Location: studentloggedin.php"); - 感觉 HTTP 标头很繁琐

标签: php mysql


【解决方案1】:

试试下面:

$result2 = mysqli_query($con,"SELECT occupation FROM tbladmin WHERE admin_usr_name    ='$username'");


$rows = $result2->num_rows;

if($rows==1) 
  header("location:studentloggedin.php");
else
 header("location:lecturerloggedin.php");

【讨论】:

  • 不需要第二个查询,你可以从第一个查询中得到那个信息,你只需要在里面加上if。
  • 你的代码有逻辑缺陷,需要检查占用而不是返回的行数。
【解决方案2】:

这是你需要的部分

if (!isset($_POST['username']) || $_POST['username'] != '') {
    // redirect back to the form
}
if (!isset($_POST['password']) || $_POST['password'] != '') {
    // redirect back to the form
}

$userName = mysqli_real_escape_string($_POST['username']); 
$password = mysqli_real_escape_string($_POST['password']);

$query = "SELECT * FROM tbladmin WHERE admin_usr_name = '$userName' AND          
      admin_pwd = '$password'";

$res = mysqli_query($query);
$numrows = mysqli_num_rows($res);

if ($numrows == 1) {
    // here you can add the code with remember me
    $dataRow = myqli_fetch_row($res);
    $username = $dataRow['admin_usr_name'];

    if ($dataRow['ocupation'] == 'student') {
        // redirect to student php file
        exit;
    } else {
        // redirect to lecturer
        exit;
    }
} else {
   // redirect to login incorrect
   exit;
}

【讨论】:

    【解决方案3】:

    您的查询有误:

    //$result2 = $mysqli_query($con,"SELECT occupation FROM tbladmin WHERE admin_usr_name    ='$username'" == 'student');
    //$row == mysqli_fetch_array($result)
    

    您不能将 = 用于某事物 == 用于其他事物。

    这是我认为应该工作的整个代码

    <?php 
    session_start();
    //*********Server Information to establish a connection ******
    
    $host        =    'localhost';
    $user        =    'kurtfarrugia';
    $dbpassword    =    '1234';
    $database    =    'kurt_farrugia';
    
    //Open connection   
    $mysqli = new mysqli($host, $user, $dbpassword, $database);
    if (mysqli_connect_errno()) 
    {
        printf("Connection failed: %s\n", mysqli_connect_error());
        exit();
    }
    
    //***************End Connection Establishment***************************************
    //*******Form Information********
    
    $userName = mysql_real_escape_string($_POST['username']); //User Name sent from Form
    $userPassword = mysql_real_escape_string($_POST['password']); // Password sent from Form
    
    // $rememberMe = strip_tags($_POST['rememberMe']);
    setcookie("username", $_POST['username']);
    
    //*********retrieving data from Database**********
    
    
    $result2 = $mysqli->query("SELECT * FROM `tbladmin` WHERE `admin_usr_name` = '".$userName."' and `admin_pwd` = '".$userPassword."'");
    if($row = $result2->fetch_assoc())
    {
        if($row['occupation'] == 'student')
        {
            // redirect logged student
        }
         else if($row['occupation'] == 'lecturer') 
        {
            // redirect logged lecturer 
        }
    } 
    else
    {
         //username doesn't exist redirect to fail page
    }
    
    //**********it ensures that the script does not continue unless the username varaible    
    is not null and not empty
    if (!empty($_POST['username'])) {
    $username = mysql_real_escape_string(strip_tags(trim($_POST['username'])));
    } else {
        //die('Hey turd, go back and fill in your username!');
            header( "Location: login_fail2.php" ); die;
        }
    ?> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-11
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      • 2016-02-11
      相关资源
      最近更新 更多