[英]php and mysql (login form)
mysql_connect('localhost','root','' ) or die(mysql_error());
mysql_select_db('user_data') or die(mysql_error());
//This code runs if the form has been submitted
if (isset($_POST['submit']))
{
//This makes sure they did not leave any fields blank
if (!$_POST['username'] || !$_post['pass'] || !$_POST['pass2'])
{
die("fill all the fields");
}
// checks if the username is in use
if(!get_magic_quotes_gpc())
{
$_POST['username']= addslashes($_POST['username']);
}
$usercheck = $_POST['username'];
$check = mysql_query("SELECT username FROM users WHERE username= '$usercheck'") or die(mysql_error());
$check2 = mysql_num_rows($check);
//if the name exists it gives an error
if($check2 != 0)
{
die('sorry, username '.$_POST['username'].' is already in use');
}
//this makes sure both passwords entered match
if($_POST['pass'] != $_POST['pass2'])
{
die('sorry, passwords did not match.');
}
// here we encrypt the password and add slashes if needed
$_POST['pass'] = md5($_POST['pass']);
if(!get_magic_quotes_gpc())
{
$_POST['pass']=addslashes($_POST['pass']);
$_POST['username']=addslashes($_POST['username']);
}
// now we insert it into the database
$insert= "INSERT INTO users (username,password) VALUES ('".$_POST['username']."','".$_POST['password']."')";
$add_member = mysql_query($insert);
?>
<h1>Registered</h1>
<p>Thank you, you have registered - you may now login</a>.</p>
<?php
}
else
{
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
<tr><td>Username:</td><td>
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="10">
</td></tr>
<tr><td>Confirm Password:</td><td>
<input type="password" name="pass2" maxlength="10">
</td></tr>
<tr><th colspan=2><input type="submit" name="submit"
value="Register"></th></tr> </table>
</form>
<?php
}
?>
這是我用於登錄的腳本,但是當我輸入名稱,passwrd並確認密碼時,由於我已經填寫了所有字段,它仍然給我顯示“填寫所有字段”的消息。 任何人都可以幫助我提前感謝
首先嘗試將!$_post['pass']
替換為!$_POST['pass']
希望這會有所幫助。
盡管代碼在許多方面都是災難性的,但對您問題的特定答案實際上是使用其他方法來檢查提交的字段。 僅通過否定它們來評估它們還不夠明確。
這樣的事情會是一個更好的開始。
if( !isset( $_POST['username'] ) || !isset( $_POST['pass'] ) || !isset( $_POST['pass2'] ) ){
die("fill all the fields");
}
這將檢查是否設置了它們。然后,您可以對這些值使用strlen()或其他內容並強制執行任何所需的規則。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.