所以我有我認爲是經過驗證和消毒的表單。當註冊商點擊提交按鈕時,它會將其帶到「標題」位置。即使註冊服務商沒有正確完成輸入,它仍然會將註冊服務商發送到「標題」位置。從我所看到的可能是兩件事之一。驗證碼不對,或者我沒有正確顯示錯誤。我將在下面留下我的代碼。服務器端驗證錯誤不顯示。用戶轉到標題位置
下面的index.php
<php include("php/processuserform.php"); ?>
<form method="POST" name="signup" action="php/processuserform.php">
<input id="firstname" onkeyup="validateFirstName()" placeholder="First Name" type="text"
value="<?php echo $valid_firstname; ?>" /><?php echo $error_firstname; ?><label
id="firstnameprompt">
</label>
<br><br>
<input id="lastname" onkeyup="validateLastName()" placeholder="Last Name" type="text" value="<?php
echo $valid_lastname; ?>" /><?php echo $error_lastname; ?><label id="lastnameprompt"></label>
<br><br>
<input id="Email" onkeyup="validateEmail()" placeholder="Email" type="text" value="<?php echo
$valid_email; ?>" /><label id="Emailprompt"><?php echo $error_email; ?></label>
<br /><br />
<input id="Password" onkeyup="validatePassword()" placeholder="Create Password" type="password"
value="<?php echo $valid_password; ?>" /><label id="Passwordprompt"><?php echo $error_password; ?>
</label>
<br /><br />
<strong>Male</strong><input id="Gender" type="radio" name="sex" value="m">
<strong>Female</strong><input id="Gender" type="radio" name="sex" value="f">
<?php echo $error_gender; ?>
<br /><br />
Click "Submit" if you agree to <a href="#">"Terms And Conditions"</a>
<br>
<input id="submit" onclick="return validateUserRegistration()" value="Submit" type="submit"
name="submit"/><label id="submitprompt"></label>
現在是processuserform.php
<?php
if($_POST)
{
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$email = $_POST['Email'];
$pw = $_POST['Password'];
$gender = $_POST['Gender'];
$data = array("$first_name","$last_name","$email","$pw",$gender);
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
//==========================================
// FirstName
if (preg_match('/^[a-zA-Z ]*$/',$first_name))
{
$valid_firstname=$first_name;
}
else
{
$error_firstname='Enter First Name.';
}
//==========================================
// Last Name
if (preg_match('/^[a-zA-Z ]*$/',$last_name))
{
$valid_lastname=$last_name;
}
else
{
$error_lastname='Enter Last Name.';
}
//==========================================
// Email
if (preg_match('/^[a-zA-Z0-9._-][email protected][a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$/', $email))
{
$valid_email=$email;
}
else
{
$error_email='Enter valid Email.';
}
//==========================================
// Password min 6 char max 20 char
if (preg_match('/^[[email protected]#$%^&*()_]{6,20}$/',$pw))
{
$valid_password=$pw;
}
else
{
$error_password='Enter valid Password min 6 Chars.';
}
//==========================================
// Gender
if ($gender==m)
{
$valid_gender==$gender;
}
if ($gender==f)
{
$valid_gender==$gender;
}
else
{
$error_gender='Select Gender';
}
//=========================================
$hostname="this is correct";
$username="this is correc";
$password="this is correc";
$dbname="this is correc";
$db_conx = mysqli_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
if(mysqli_connect_errno()){
echo mysqli_connect_error();
exit();
}
$select = mysqli_select_db($db_conx,$dbname);
if((strlen($valid_firstname)>0)&&(strlen($valid_lastname)>0)
&&(strlen($valid_email)>0)&&(strlen($valid_password)>0) && $valid_gender==m||f)
{
mysqli_query($db_conx,"INSERT INTO users (firstname, lastname, email, password, gender)
VALUES ('$first_name', '$last_name', '$email', '$pw', '$gender')");
mysqli_close($db_conx);
header("Location: pages/profile.php");
}
else{ }
}
?>
我有點過時與我的PHP的,但我不看看'$ valid_gender == m || f'是如何有效的。 'm'和'f'不是字符串,這就是輸入的形式,我不認爲你可以這樣使用OR條件,它通常是'field == val ||'。字段== otherVal'。由於條件沒有分組,因此您的陳述可能總是等於真實 – helion3