在php中刷新页面后如何避免错误

时间:2014-08-02 21:57:14

标签: php

实际上这是关于php中的一个简单查询我有一个简单的html登录表单,如下面的post方法和数据库中的登录表,字段用户名密码...等我能够获得所需的结果但是当我得到结果我刷新该页面并出现错误

Notice: Undefined index: usrname in C:\xampp\htdocs\tut\pro\ex1.php on line 9

Notice: Undefined index: password in C:\xampp\htdocs\tut\pro\ex1.php on line 10

.........我想知道的是如何在刷新页面后避免此错误有任何方法可以这样做。

 <html><body>
<form method="post" action="ex1.php">
username:<input type="text" name="usrname" value=""><br>
passsword:<input type="password" name = "password" value=""><br>
<input type="submit" name="submit" value="sign up">
</form>
</html>
</body>

ex1.php如下:

 <?php
$cxn= mysqli_connect("localhost","root","","movie");
if(mysqli_connect_errno())
{
echo" error connecting ".mysqli_connect_error();
}

if (isset($_POST)) {
$username=$_POST['usrname'];
$password=$_POST['password'];
$sql="select * from login where username= '$username' and password= '$password'";
$result=mysqli_query($cxn,$sql);
unset($_POST);


if($result)
{
if(mysqli_num_rows($result) == '1')
{
echo "<h1>welcome &nbsp".$username."&nbsp on your account</h1>";
}}}
?>

1 个答案:

答案 0 :(得分:0)

尝试更改此内容,

username:<input type="text" name="usrname"><br>
passsword:<input type="password" name = "password"><br>

$username=$_POST['username'];
$password=$_POST['password'];
$sql="select * from login where username= '$username' and password= '$password'";
$result=mysqli_query($cxn,$sql);

到此..

username:<input type="text" name="usrname" value=""><br>
passsword:<input type="password" name = "password" value=""><br>

if (isset($_POST)) {
$username=$_POST['username'];
$password=$_POST['password'];
$sql="select * from login where username= '$username' and password= '$password'";
$result=mysqli_query($cxn,$sql);
unset($_POST);
}

然后,只有在用户提交表单时才会创建变量。