undefined index text fields php database?

匿名 (未验证) 提交于 2019-12-03 02:42:02

问题:

when i enter the url(//login.php) after login it gives undefined index on t1 and t2. how can i use session on this page
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> 

this is 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 }  

this is error Notice: Undefined index: t1 in C:\wamp\www\12dec\core\login.php on line 8 Notice: Undefined index: t2 in C:\wamp\www\12dec\core\login.php on line 9

all is working fine .but when i re-enter the url it gives this error. and how to fix this

回答1:

In your login.php script , try changing

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

to

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

and then handle blank $user and/or blank $pass .

If either $user is blank or $pass is blank , either you can display a message to the user or display the login page which seems to be index.php in your case .



回答2:

it happened because the $_POST haven't index named 't1' or 't2', you can try ternary operation like this :

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

it means like this :

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!