简体   繁体   English

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

[英]how to avoid error after refreshing the page in php

actually this is regarding a simple query in php i have a simple html login form as follow with post method and login table in the database with fields username password ...etc i am able to get the desire result but when i get the result i refresh that page and an error come 实际上,这是关于php中的简单查询的,我有一个简单的html登录表单,其后是数据库中具有字段用户名密码... etc的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

.........what i want to know is how can i avoid this error after refreshing the page is there any method to do so. ....我想知道的是刷新页面后如何避免此错误,是否有任何方法可以这样做。

 <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 is as follow : 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>";
}}}
?>

Try changing this, 尝试改变这个,

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);

to this.. 对此。

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);
}

Then your variables will only be created when the user submits the form. 然后,仅当用户提交表单时才创建变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM