Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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_Html_Mysql_Sql_Forms - Fatal编程技术网

Php 登录表单脚本其他部分不工作

Php 登录表单脚本其他部分不工作,php,html,mysql,sql,forms,Php,Html,Mysql,Sql,Forms,我为我的web应用程序编写了一个简单的登录表单脚本。事情进展顺利。如果我的用户名和密码匹配,我就可以登录到该帐户 我的问题是,如果输入的详细信息无效,它不会显示所需的消息 剧本如下 <?php include 'buildconfig.php'; $connection = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL:" . mysql_error()); $db=mysql_

我为我的web应用程序编写了一个简单的登录表单脚本。事情进展顺利。如果我的用户名和密码匹配,我就可以登录到该帐户

我的问题是,如果输入的详细信息无效,它不会显示所需的消息

剧本如下

<?php
include 'buildconfig.php';

$connection = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or
die("Failed to connect to MySQL:" . mysql_error());
$db=mysql_select_db(DB_NAME,$connection) or 
die("Failed to connect to MySQL: ".mysql_error());

function SignIn()
{
    // username and password sent from form
    $myusername=$_POST['username'];
    $mypassword=$_POST['password'];

    // To protect MySQL injection (more detail about MySQL injection)
    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);

    $sql="SELECT * FROM members WHERE uname='$myusername' and password='$mypassword'";
    $result = mysql_query($sql);
    if($result === FALSE) {
        die(mysql_error()); 
    }

    while($row = mysql_fetch_array($result))
    {
        if($row["uname"] == $myusername && $row["password"] == $mypassword)
        {
            $to_location = "Location:../pages/login.html";header($to_location);
        }
        //this else is not working
        else
        {
            echo"Sorry, your credentials are not valid, Please try again.";
        }
    }
}

if (isset($_POST['submit']))
{
    SignIn();
}
?>

当代码不匹配时,您将无法获得任何行,要检查该行是否存在,请使用mysqli_num_行

if(mysql_num_rows($result)!==0)
使用下面的代码

<?php
include 'buildconfig.php';

$connection = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or
die("Failed to connect to MySQL:" . mysql_error());
$db=mysql_select_db(DB_NAME,$connection) or 
die("Failed to connect to MySQL: ".mysql_error());

function SignIn()
{
// username and password sent from form
$myusername=$_POST['username'];
$mypassword=$_POST['password'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM members WHERE uname='$myusername' and password='$mypassword'";
$result = mysql_query($sql);
if($result === FALSE) {
die(mysql_error()); 
}
if(mysql_num_rows($result)!==0){
while($row = mysql_fetch_array($result))
{
if($row["uname"] == $myusername && $row["password"] == $mypassword)
{$to_location = "Location:../pages/login.html";header($to_location);}

}
}
//this else is working
else
{echo"Sorry, your credentials are not valid, Please try again.";}
}


if (isset($_POST['submit']))
{SignIn();}
?>

上面的TJ是正确的。您的错误回音位于永远不会运行的while循环中。只需将它显示出来,您甚至不需要if,因为您的头应该在到达else语句之前重定向if success

<?php
    include 'buildconfig.php';

    $connection = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or
    die("Failed to connect to MySQL:" . mysql_error());
    $db=mysql_select_db(DB_NAME,$connection) or 
    die("Failed to connect to MySQL: ".mysql_error());

    function SignIn(){
        // username and password sent from form
        $myusername=$_POST['username'];
        $mypassword=$_POST['password'];

        // To protect MySQL injection (more detail about MySQL injection)
        $myusername = stripslashes($myusername);
        $mypassword = stripslashes($mypassword);
        $myusername = mysql_real_escape_string($myusername);
        $mypassword = mysql_real_escape_string($mypassword);

        $sql="SELECT * FROM members WHERE uname='$myusername' and password='$mypassword'";
        $result = mysql_query($sql);
        if($result === FALSE) {
            die(mysql_error()); 
        }

        while($row = mysql_fetch_array($result)){
            if($row["uname"] == $myusername && $row["password"] == $mypassword){
                $to_location = "Location:../pages/login.html";
                header($to_location);
            }
        }

        // this will run after the while loop and in theory the header redirect so no need for else
        echo "Sorry, your credentials are not valid, Please try again.";
    }

    if (isset($_POST['submit'])){
        SignIn();
    }
?>

如果不匹配,则不存在
$row
,因此它不会进入while循环。put ob_start();请查查我的答案为什么投反对票?不酷,因为这是一个有效的正确答案。这个答案也适用于我。。。非常感谢。说不出它为什么会倒下votedHa,重要的是它帮助回答了这个问题并让你前进:)