未定义的索引文本字段php数据库? [英] undefined index text fields php database?
问题描述
当我输入url(// login.php)登录后,它给t1和t2上的未定义索引。
如何在此页面上使用会话
index.php
< head&
< link rel =stylesheettype =text / csshref =css / style.css>< / head>
< h1>在此登录< / h1>
< form name =f1action =login.phpmethod =post>
< table border =1>
< tr>< td> username< / td>< td>< input type =textname =t1>< / td>
< / tr>
< tr>< td> password< / td>< td>< input type =passwordname =t2>< / td>< / tr>
< tr>< td>< input type =submitvalue =login>< / td&
< td class =error><?php if(isset($ _ GET ['wrong_detial'])){
echoRE-ENTER username and password !! }?>
< / td>
< / tr>
< / table>
< / form>
这是login.php
<?php
includedb / db.php;
$ user = $ _ POST ['t1'];
$ pass = $ _ POST ['t2'];
$ result = mysql_query(select * from registor where username ='$ user'and password ='$ pass')或die(mysql_error());
$ row = mysql_fetch_row($ result);
?>
< h1>欢迎先生<?php echo $ user;?>< / h1>
<?php
if(!$ user == $ row [1] ||!$ pass == $ row [2]){//如果用户名或密码与数据库不匹配
header(location:account.php?wrong_detial);
}
else {
if($ user =='admin'){//如果管理员登录
?
< table border =1>
< tr>< td>使用者名称< / td>< td>使用者名称< / 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> <! - 删除单个记录 - >
<?php if(isset($ _ GET ['user_del'])){
echorecord deleted;
}
?>
< / td>
< / tr>
<?php
}
?>
< / table>
<?php}
这是错误
注意: t1 in C:\wamp\www\12dec\core\login.php on line 8
注意:未定义的索引:t2在C:\wamp\www\12dec\core\\ \\ login.php on line 9
所有都工作正常,但当我重新输入url它给出这个错误。以及如何解决这个问题,因为$ _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 ='';
}
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
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 = '';
}
这篇关于未定义的索引文本字段php数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!