dougou8552 2013-05-11 17:27
浏览 14
已采纳

当密码和用户名在DB中匹配时,错误显示?

when write anything in form and click Enter button, there is no error, but when i write the username and password correct the error show up i tried echo "$_POST['username']"; // print username if username doesn't match ?

Error:

Notice: Undefined index: username in ..

Notice: Undefined index: password in ..

this is my form

<form action="2.php" method="post">


<table align="center">
    <tr>
    <td>Username</td>
    <td><input type="text" name="username" /></td>
    </tr>
    <tr>
    <td>Password</td>
    <td><input type="password" name="password" /></td>
    </tr>
    <tr>
    <td colspan="2" align="center"><input type="submit" value="Enter"  />
    </td>
    </tr>
</table>
</form>

and this my second page

<?php
$username = mysql_real_escape_string($_POST['username']); 
$password = mysql_real_escape_string($_POST['password']);

    $connection = mysql_connect('localhost', 'root', '');
    if (!$connection){
        die('Could not connect');
        exit; 
    }
    mysql_select_db('dbName') or die( "Unable to select database");

    $query = "SELECT * FROM admin WHERE username = '$username'";

    $result = mysql_query($query) or die(mysql_error());

    $num = mysql_num_rows($result); // number of rows

    if ($num > 0){
    $i = 0;
    while ($i < $num){


        $row = mysql_fetch_array($result);

        if ( ($password) == $row['password'] && ($username) == $row['username'] ){

            header('location:2.php');

            $_SESSION['sessionname'] = $username;
            $_SESSION['sessionpass'] = $password;
        }

        elseif ( ($password) != $row['password'] && ($username) == $row['username'] ) {
            echo "Wrong Password <a href='1.php' >Click Here</a>";

        }

        $i++;
        }
    }else {
        echo "Username  <strong><u>$_POST[username]</u></strong> invalid ! <a href='1.php' >Click Here</a> ";


        }
    ?>
  • 写回答

1条回答 默认 最新

  • doumo2501 2013-05-11 17:29
    关注

    There is no reason to assign the username and password variables in the way you did. Simply assign the post data to the variables as you normally would with a session.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上