Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.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
在这个if语句(PHP)中,只有echo在这段代码中起作用_Php_If Statement - Fatal编程技术网

在这个if语句(PHP)中,只有echo在这段代码中起作用

在这个if语句(PHP)中,只有echo在这段代码中起作用,php,if-statement,Php,If Statement,在这段代码中,if语句的内容(if($password==$data[2])没有正确运行:只有echo函数工作,其他所有函数都不工作 这个问题有解决办法吗 我使用PHP7.0 职位: echo“测试”仅为测试目的而添加,无论是否使用它,代码都不起作用 查看此部分: $con = mysqli_connect('localhost', 'login', '*mypass*', 'login'); $check = "SELECT * FROM users"; $rs = mysqli_que

在这段代码中,if语句的内容(if($password==$data[2])没有正确运行:只有echo函数工作,其他所有函数都不工作

这个问题有解决办法吗

我使用PHP7.0

职位:

  • echo“测试”
    仅为测试目的而添加,无论是否使用它,代码都不起作用
查看此部分:

$con = mysqli_connect('localhost', 'login', '*mypass*', 'login');

$check = "SELECT * FROM users";

$rs = mysqli_query($con, $check);

if(isset($_POST['submit'])) {

            $username = strtolower($_POST['username']);

            $password = hash('sha512', $_POST['password']);

            if($username && $password){
                while($data = mysqli_fetch_array($rs)){
                    if($username == $data[1]){
                        if($password == $data[2]){
                            session_start();
                            $_SESSION['loggedin'] = true;
                            $_SESSION['username'] = $username;

                            $exp = time() + (60*60*24*365);
                            setcookie('user', $username, $exp);
                            echo "test";
                            header("location: dash.php");
                            exit();
                        } else {
                            error("Incorrect Password");
                        }
                    }
                }
            } else {
                error("Please insert your username and password");
            }
        }
阅读
$data
时应小心。简而言之,
$data
数组是从0开始按行索引的,因此
$data[1]
$data[2]
引用整个第二和第三个结果集行,但实际上没有读取任何结果字段

正确和更干净的方法是:

 if($username && $password){
                while($data = mysqli_fetch_array($rs)){
                    if($username == $data[1]){
                        if($password == $data[2]){

如果您宁愿远离foreach循环,您可以使用while或for,但您需要定义一个交互计数器(即在进入循环之前,
$i=0;
,以及在每次迭代结束之前,
$i++;
以及每次通过访问
$data[$i]['field\u name\u with\u username']
$data[$i]['field\u name\u with\u password']

这不是答案,但可以尝试检查会话是否正常工作

将以下代码放在其自己的文件中:

if($username && $password){
  $data = mysqli_fetch_array($rs);
  foreach ($data as $row)
  {
    $username = $row['field_name_with_username];
    $password = $row['field_name_with_password];

    ... then whatever you need to do afterwards with those variables

  }

mysqli\u fetch\u数组默认为您提供索引的数字或字段名选择。这是一个品味问题。不需要获取一行,然后在此处迭代该行(我怀疑它会起作用,因为键不存在)。看起来您将
mysqli\u fetch\u数组
mysqli\u fetch\u all
混淆了。
<?php
session_start();
if(isset($_SESSION['test_counter'])) {
    $_SESSION['test_counter']++;
} else {
    $_SESSION['test_counter'] = 1;
}    

echo $_SESSION['test_counter'];