PHP form,how to echo on the same page?
ok here is the situation , I want to use a login form for e.g.
<form name="form1" method="post" action="sign.php">
Username:<br/>
<input name="myusername" type="text" id="myusername" /><br />
Password:<br />
<input name="mypassword" type="text" id="mypassword" /><br />
<input type="submit" name="Submit" value="Login" />
</form>
sign.php
<?php
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="cosmos"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$mypassword = md5($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $my开发者_如何学Pythonpassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>
In case if the user enters wrong details then I want this to appear on the same page of the form some thing like... "Wrong Username or Password , Please try again..."and we have form at bottom
Test to see if a POST has occurred. If so then verify the credentials; if correct then redirect to the destination page; if incorrect then display the error message. Then output the form.
精彩评论