Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将数据库的内容与输入进行比较?_Php_Mysql_Sql_Database_Session - Fatal编程技术网

Php 如何将数据库的内容与输入进行比较?

Php 如何将数据库的内容与输入进行比较?,php,mysql,sql,database,session,Php,Mysql,Sql,Database,Session,我知道我可以连接到数据库。我在修改代码之前已经检查过了 当管理员登录时,我想重定向到tableedit.php,但如果用户不是管理员,我想重定向到tableuser.php 这就是错误: 警告:mysql\u fetch\u array()希望参数1是资源,字符串在第47行的C:\xampp\htdocs\joy\inventory\login.php中给出 查询失败 代码如下: //Create query $qry="SELECT * FROM user WHERE username='$u

我知道我可以连接到数据库。我在修改代码之前已经检查过了

当管理员登录时,我想重定向到
tableedit.php
,但如果用户不是管理员,我想重定向到
tableuser.php

这就是错误:

警告:mysql\u fetch\u array()希望参数1是资源,字符串在第47行的C:\xampp\htdocs\joy\inventory\login.php中给出 查询失败

代码如下:

//Create query
$qry="SELECT * FROM user WHERE username='$username' AND password='".md5($_POST['password'])."'";

while($row=mysql_fetch_array($qry));

$result = $row['username'];
{
//Check whether the query was successful or not
if($result) {
    if(mysql_num_rows($result)== 'admin'){
        session_regenerate_id();
        $member = mysql_fetch_assoc($result);
        $_SESSION['SESS_MEMBER_ID'] = $member['id'];
        $_SESSION['SESS_FIRST_NAME'] = $member['username'];
        $_SESSION['SESS_LAST_NAME'] = $member['password'];
        session_write_close();
        header("location: tableedit.php");
        exit();
    }
    elseif(mysql_num_rows($result) != 'admin') {
        //Login Successful   
        session_regenerate_id();
        $member = mysql_fetch_assoc($result);
        $_SESSION['SESS_MEMBER_ID'] = $member['id'];
        $_SESSION['SESS_FIRST_NAME'] = $member['username'];
        $_SESSION['SESS_LAST_NAME'] = $member['password'];
        session_write_close();
        header("location: tableuser.php");
        exit();
    } else {
        //Login failed
        $errmsg_arr[] = 'Username and password not found.';
        $errflag = true;
        if($errflag) {
            $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
            session_write_close();
            header("location: index.php");
            exit();
        }
    }
} else {
    die("Query failed");
}}

您应该首先执行mysql\u查询并将结果放入数组中。在此之后:

   $qry="SELECT * FROM user WHERE username='$username' AND
         password='".md5($_POST['password'])."'";
这样做:

$result = mysql_query($qry);
其次,

此外,建议您将salt与md5一起使用,以获得良好的安全性。进一步补充,,
您可以使用sha1实现更好的安全性

mysql\u fetch\u数组需要一个mysql资源作为参数。资源是从mysql\u查询返回的,所以应该是

$qry = 'YOUR QUERY';
$res = mysql_query($qry);
while ($row = mysql_fetch_array($res))
而且。。。mysql_num_rows永远不会返回'admin'(它返回整数,即给定资源中的行数),因此您将始终被重定向到tableuser.php

在这种情况下,你不需要“elseif”,“else”在这里就好了:)


那么,为什么要使用mysql_*函数呢?试试PDO:)

您的代码有很多问题。。。很多
mysql.*
函数的工作方式与您似乎认为的不同,您使用哪些资源来学习

对于大多数php函数,您可以访问:

示例:

  • 代码 下面的代码应该可以帮助您开始。您可以在相关位置添加所有的
    $\u会话
    内容(请参见注释)

    mysqli:: <>我强烈建议您也考虑更改为<代码> MySqLI<代码>(或<代码> PDO < /代码>)如果您要这样做,以下内容将帮助您开始:

    $mysqli = new mysqli('localhost', 'USERNAME', 'PASSWORD', 'DATABASE'); //Replace USERNAME, PASSWORD, DATABASE to the actual database username etc.
    
    $password = md5($password);
    $qry= $mysqli->query("SELECT * FROM user WHERE username='$username' AND password='$password'");
    if($qry->num_rows){
        while($row = $qry->fetch_assoc()){
            if($row['uername'] == 'admin'){
                //The user IS the admin
                header('location:tableedit.php');
            }
            else{
                //The user is NOT admin
                header('location:tableuser.php');
            }
        }
    }
    else{
        //Login failed
        header('location:/');
    }
    
    exit; //Only need this once at the end
    
    有关更多信息,请查看文档:


  • 坦率地说,代码完全是一团糟。在寻求帮助之前,请尝试阅读php手册
    $password = md5($password);
    $qry= mysql_query("SELECT * FROM user WHERE username='$username' AND password='$password'");
    if(mysql_num_rows($qry)){
        while($row = mysql_fetch_array($qry)){
            if($row['uername'] == 'admin'){
                //The user IS the admin
                header('location:tableedit.php');
            }
            else{
                //The user is NOT admin
                header('location:tableuser.php');
            }
        }
    }
    else{
        //Login failed
        header('location:/');
    }
    
    exit; //Only need this once at the end
    
    $mysqli = new mysqli('localhost', 'USERNAME', 'PASSWORD', 'DATABASE'); //Replace USERNAME, PASSWORD, DATABASE to the actual database username etc.
    
    $password = md5($password);
    $qry= $mysqli->query("SELECT * FROM user WHERE username='$username' AND password='$password'");
    if($qry->num_rows){
        while($row = $qry->fetch_assoc()){
            if($row['uername'] == 'admin'){
                //The user IS the admin
                header('location:tableedit.php');
            }
            else{
                //The user is NOT admin
                header('location:tableuser.php');
            }
        }
    }
    else{
        //Login failed
        header('location:/');
    }
    
    exit; //Only need this once at the end