Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.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 $data=mysqli\u fetch\u数组($result)不工作_Php_Html_Mysql - Fatal编程技术网

Php $data=mysqli\u fetch\u数组($result)不工作

Php $data=mysqli\u fetch\u数组($result)不工作,php,html,mysql,Php,Html,Mysql,我试图用php制作一个登录页面,连接mysql数据库。。下面是登录页面的html代码,在登录页面中输入值,然后指向php的第二个页面进行检查 <html> <head> <title>Library Login</title> <link rel="stylesheet" type="text/css" href="css/reset.css"> <link rel="stylesheet" type="t

我试图用php制作一个登录页面,连接mysql数据库。。下面是登录页面的html代码,在登录页面中输入值,然后指向php的第二个页面进行检查

<html>
<head>
    <title>Library Login</title>
    <link rel="stylesheet" type="text/css" href="css/reset.css">
    <link rel="stylesheet" type="text/css" href="css/structure.css">
</head>
<body>
    <form class="box login" method="GET" action="http://localhost/redirect.php">
        <label align="center"><font size="6" color="grey">Library System</font></label>
        <fieldset class="boxBody">
        <label>Username</label>
        <input type="text"  placeholder="Username" required name="username">
        <label><a href="#" class="rLink" tabindex="5" ></a>Password</label>
        <input type="password" placeholder="Password" required name="password">
        <input type="submit" class="btnLogin" value="Login"  name="login">
        <input type="reset" class="btnLogin" value="Reset"  name="reset" >
        <label>
    </form>
</html>
</div>
下面是第二页的代码,无论输入什么条目,都只执行else条件。。。我是新的Php和Mysql。。。请帮帮我

<?php
$con=mysqli_connect("localhost","root","","project");

if(mysqli_connect_errno())
{
    echo "failed".mysqli_connect_errno();
}

$uid=$_GET['username'];
$pass=$_GET['password'];
$sql="SELECT *FROM login";
$result=mysqli_query($con,$sql);

while($data=mysqli_fetch_array($result))
{
    if($uid==$data['user'] and $pass==$data['pass'])
    {
        header('location:http://localhost/error/index.html');
    }
    else
    {
        header('location:http://localhost/mam.html');
    }
}

mysqli_close($con);
?>

好的,在处理身份验证时,让我们稍微改进一下代码

<?php

// Do not connect using root, especially when not setting a password:
$con=mysqli_connect("localhost","projectuser","password","project");
if(mysqli_connect_errno())
{
echo "failed".mysqli_connect_errno();
}

$uid = $_GET['username'];
$pass = $_GET['password'];

// This is the main problem, there was a typo:
$sql = "SELECT * FROM login";

// Directly ask the DB if the credentials are correct.
// Then you do not need the loop below.
// BUT: Do not forget to escape the data in this case!
$sql .= " WHERE uid = '" . mysqli_real_escape_string($uid) . "' AND pass = '" . mysqli_real_escape_string($pass) . "'";

$result=mysqli_query($con,$sql);
if ($result->num_rows === 1) {
    header('location:http://localhost/mam.html');
} else {
    header('location:http://localhost/error/index.html');
}
mysqli_close($con);
?>
进一步的改进是对数据库中的密码进行散列和加密


此外,正如VMai所指出的,使用准备好的报表是合适的。

No。。每次在检查值时执行else条件,无论我是否输入了正确的输入,我知道这不是答案,但您确实应该尝试移动到,而不是mysqli。如果结果的第一行不匹配,然后,您将进入else部分,通过header调用,您将转到mam.html,不再返回此脚本。如果存在相关行,则应使用WHERE子句获取相关行。在将输入值放入WHERE子句之前,您应该先阅读带有占位符的准备语句;ini设置“显示错误”,1;看看它是否能产生任何效果。@ceakki:PDO是一个抽象层,有助于在更高的层次上进行推理,而mysqli是特定于实现的,并与MySQL实现细节建立耦合。一般来说,在开源社区中,mysqli被认为被PDO所取代。再看看这里:mysql\u real\u escape\u字符串,这是一个mysql\u函数;小心-您将OP使用的mysqli_*函数与mysql_*函数混合使用。那不行。请使用准备好的语句和占位符改进mysqli行中的代码,并将输入绑定到占位符,而不是字符串连接。缺少i,修复了该问题。但是,如果两个模块都可用,它会起作用-至少对于转义来说是这样。但是,如果两个模块都可用,它会起作用-你能详细说明一下吗?mysql_uu和mysqli_uuu不会在使用的相同页面/代码中混合在一起。这些是独立的MySQL API,PDO也是。