未定义索引文本字段php数据库?

时间:2013-12-19 02:31:18

标签: php mysql database

当我在登录后输入url(// login.php)时,它会在t1和t2上给出未定义的索引。 我怎样才能在这个页面上使用会话
的index.php

        <head>
<link rel="stylesheet" type="text/css" href="css/style.css"></head>
    <h1>Login Here</h1>
    <form name="f1" action="login.php" method="post">
    <table border="1">
    <tr><td>username</td><td><input type="text" name="t1"></td>
    </tr>
    <tr><td>password</td><td><input type="password" name="t2"></td></tr>
    <tr><td><input type="submit" value="login"></td>
    <td class="error"><?php if(isset($_GET['wrong_detial'])){
        echo "RE-ENTER username and password !! "; }?>
    </td>
    </tr>
    </table>
</form>

这是login.php

     <?php
    include "db/db.php";
    $user=$_POST['t1'];
    $pass=$_POST['t2'];
     $result=mysql_query("select * from registor where username='$user' and password='$pass' ")or die(mysql_error());
    $row=mysql_fetch_row($result);
    ?>
    <h1>Welcome Mr. <?php echo $user;?></h1>
   <?php
    if(!$user == $row[1] || !$pass ==$row[2]){     //if username or password not matched with database
        header("location:account.php?wrong_detial");
    }
    else{
        if($user == 'admin'){     //if admin login
    ?>
    <table border="1">
    <tr><td>User ID</td><td>Username</td><td>Password</td><td>Email-ID</td><td>delete</td></tr>
    <?php
   $admin_result=mysql_query("select * from registor");
    while($admin_db=mysql_fetch_row($admin_result)){
        ?>
    <tr>
    <td><?php echo $admin_db[0];?></td>
    <td><?php echo $admin_db[1];?></td>
    <td><?php echo $admin_db[2];?></td>
    <td><?php echo $admin_db[3];?></td>
    <td id="div1">
    <a href="delete.php?delete=<?php echo $admin_db[1];?>" id="<?php echo $admin_db[1];?>">Delete</a> <!-- Deleting single record-->
    <?php   if(isset($_GET['user_del'])){
    echo "record deleted";
    }
        ?>
    </td>
    </tr>
    <?php
    }
    ?>
    </table>
    <?php } 

这是错误 注意:未定义的索引:第8行的C:\ wamp \ www \ 12dec \ core \ login.php中的t1 注意:未定义的索引:第9行的C:\ wamp \ www \ 12dec \ core \ login.php中的t2

一切正常。但是当我重新输入网址时,它会出现此错误。以及如何解决这个问题

2 个答案:

答案 0 :(得分:0)

login.php脚本中,尝试更改

$user=$_POST['t1'];
$pass=$_POST['t2'];

$user = isset( $_POST['t1'] ) ? $_POST['t1'] : '';
$pass = isset( $_POST['t2'] ) ? $_POST['t2'] : '';

然后处理空白$user和/或空白$pass

如果$user为空或$pass为空,您可以向用户显示消息或显示您的案例中似乎为index.php的登录页面。

答案 1 :(得分:0)

发生这种情况是因为$ _POST没有名为't1'或't2'的索引,你可以尝试这样的三元运算:

<?php
$user = isset($_POST['t1']) ? $_POST['t1'] : '';
$pass = isset($_POST['t2']) ? $_POST['t2'] : '';

这意味着:

if (isset($_POST['t1'])) {
    $user = $_POST['t1'];
} else {
    $user = '';
}