Php 我的代码没有';由于我的表单验证,无法插入到数据库中
每次我使用下面的方法将表单插入数据库时,它都不会做任何事情Php 我的代码没有';由于我的表单验证,无法插入到数据库中,php,Php,每次我使用下面的方法将表单插入数据库时,它都不会做任何事情 <form action="<?= $_SERVER['PHP_SELF']; ?>" method="post" class="form-horizontal"> <div class="form-group"> <label for="first_name" class="col-sm-4 control-label" >First Name:</lab
<form action="<?= $_SERVER['PHP_SELF']; ?>" method="post" class="form-horizontal">
<div class="form-group">
<label for="first_name" class="col-sm-4 control-label" >First Name:</label>
<div class="col-sm-8">
<input type="text" name="first_name" class="form-control" id="first_name" value="<?= $first_name; ?>"/>
<span id="error-msg"><?= $first_name_error; ?></span>
</div>
</div>
<div class="form-group">
<label for="last_name" class="col-sm-4 control-label">Last Name:</label>
<div class="col-sm-8">
<input type="text" name="last_name" class="form-control" maxlength="30" id="last_name" value="<?= $last_name; ?>"/>
<span id="error-msg"><?= $last_name_error; ?></span>
</div>
</div>
<div class="form-group">
<label for="email_address" class="col-sm-8 control-label">Email Address:</label>
<div class="col-sm-8">
<input type="text" name="email_address" class="form-control" id="email_address" placeholder="abc@email.com" value="<?= $email_address; ?>"/>
<span id="error-msg"><?= $email_address_error; ?></span>
</div>
</div>
<div class="form-group">
<label for="user_name" class="col-sm-4 control-label" >Username:</label>
<div class="col-sm-8">
<input type="text" name="username" class="form-control" maxlength="30" id="user_name" value="<?= $username; ?>"/>
<span id="error-msg"><?= $username_error; ?></span>
</div>
</div>
<div class="form-group">
<label for="country" class="col-sm-4 control-label">Phone:</label>
<div class="col-sm-8">
<input type="tel" id="phone" name="phone" class="form-control" value="<?= $phone; ?>"/>
<span id="valid-msg" class="hide">► </span>
<span id="error-msg" class="hide"><?= $phone_error; ?></span>
</div>
</div>
<div class="form-group">
<label for="pass_word" class="col-sm-4 control-label" >Password:</label>
<div class="col-sm-8">
<input type="password" name="password" class="form-control" maxlength="30" id="password" value="<?= $password; ?>"/>
<span id="error-msg"><?= $password_error; ?></span>
</div>
</div>
<div class="form-group">
<label for="confirm_password" class="col-sm-4 control-label" >Confirm password:</label>
<div class="col-sm-8">
<input type="password" name="confirm_password" class="form-control" maxlength="30" id="confirm_password" value="<?= $confirm_password; ?>"/>
<span id="error-msg"><?= $confirm_password_error; ?></span>
</div>
</div>
<div class="col-sm-8 col-sm-push-3">
<input type="submit" name="submit" class="btn bg-success" value="Register" onClick="return confirm('Are you sure your details are correct?');" />
</div>
</form>
<?php
//define variables and set them to empty values
$first_name = $last_name = $country = $phone = $email_address = $username = $password = $confirm_password = "";
$first_name_error = $country_error = $last_name_error = $phone_error = $email_address_error = $username_error = $password_error = $confirm_password_error = "";
$timestamp = strftime("%Y-%m-%d %H:%M:%S", time());
//form is submitted with post method
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(empty($_POST["first_name"])){
$first_name_error = "<div class=''>First Name is required</div>";
}else{
$first_name = test_input($_POST["first_name"]);
//Check if name only contains letters and whitespaces
if(!preg_match("/^[a-zA-Z ]*$/",$first_name)){
$first_name_error = "<div class=''>Only letters and white space allowed</div>";
}
}
if(empty($_POST["last_name"])){
$last_name_error = "<div class=''>Last Name is required</div>";
}else{
$last_name = test_input($_POST["last_name"]);
//Check if name only contains letters and whitespaces
if(!preg_match("/^[a-zA-Z ]*$/",$last_name)){
$last_name_error = "<div class=''>Only letters and white space allowed</div>";
}
}
if(empty($_POST["email_address"])){
$email_address_error = "<div class=''>Email is required</div>";
}else{
$email_address = test_input($_POST["email_address"]);
// check if email address is well formed
if(!filter_var($email_address, FILTER_VALIDATE_EMAIL)){
$email_address_error = "<div class='btn bg-warning'>Invalid email format</div>";
}elseif($email_address = test_input($_POST["email_address"])){
$sql = "SELECT email_address FROM customers WHERE email_address = '$email_address'";
$mail = $database->query($sql);
if(mysqli_num_rows($mail) > 0){
$email_address_error = '<div class="">ERROR: Email already exists please use another email</div>';
}
}
}
if(empty($_POST["username"])){
$username_error = "<div class=''>Username is required</div>";
}else {
$username = test_input($_POST["username"]);
//check if username is atleast 7 characters
if(!preg_match("/^(?=.*?[a-z]).{7,}$/",$username)){
$username_error = "<div class=''>Username must be atleast 7 characters</div>";
}elseif($username = test_input($_POST["username"])){
$sql = "SELECT username FROM customers WHERE username = '$username'";
$user = $database->query($sql);
if(mysqli_num_rows($user) > 0){
$username_error = '<div class="">ERROR: Username already exists please use another username</div>';
}
}
}
if(empty($_POST["phone"])){
$phone_error = "<div class=''>Phone is required</div>";
}else {
$phone = test_input($_POST["phone"]);
}
if(empty($_POST["password"])){
$password_error = "<div class=''>Password is required</div>";
}else{
$password = test_input($_POST["password"]);
//check if password is atleast 7 characters
if(!preg_match("/^(?=.*?[a-z]).{7,}$/",$password)){
$password_error = "<div class=''>Password must be atleast 7 characters</div>";
}
}
if(empty($_POST["confirm_password"])){
$confirm_password_error = "<div class=''>Alternate password is required</div>";
}else{
$confirm_password = test_input($_POST["confirm_password"]);
//check if cpassword is atleast 8 characters
if(!preg_match("/^(?=.*?[a-z]).{7,}$/",$confirm_password)){
$confirm_password_error = "<div class=''>Password must be atleast 7 characters</div>";
}else{
if($_POST['confirm_password'] != $password){
$confirm_password_error = "<div class=''>Password does not match!!!</div>";
}
}
}
if($first_name_error = "" and $last_name_error = "" and $mobile_number_error = "" and $email_address_error = "" and $username_error = "" and $password_error = "" and $confirm_password_error = ""){
$str = '1234567890asdf';
$str = str_shuffle($str);
$str = substr($str, 0, 10);
$token = 'vfjhvbkebecbjDRCWVJEcbkrvlnke24tir7c_zdvbejw968';
$token = str_shuffle($token);
$token = substr($token, 0, 10);
$user = new Customer_reg();
$password = sha1($password);
$user->customer_id = $str;
$user->first_name = $first_name;
$user->last_name = $last_name;
$user->email_address = $email_address;
$user->username = $username;
$user->password = $password;
$user->mobile_number = $phone;
$user->created_at = $timestamp;
$user->updated_at = $timestamp;
$user->emailConfirm = 0;
$user->token = $token;
$user->str = $str;
if($user->save()){
$mail = new Mail();
$mail->email_address = $email_address;
$mail->token = $str;
$mail->send_verification();
$session->message('<div class="btn bg-success">Account created sucessfully please verify your email.</div>');
redirect_to('login.php');
}
}
if(empty($_POST["message"])){
$message = "";
} else{
$message = test_input($_POST["message"]);
}
}
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = htmlentities($data);
return $data;
}
?>
请问,我如何修改这个?除上述代码外,我的所有函数和所有其他代码都可以正常工作。请确保定义$mobile\u number\u错误,然后再次尝试执行代码并查看。首先,您应该在此处使用“==”而不是“=”和“&&&”而不是“和” 即改变这一点:
if($first_name_error = "" and $last_name_error = "" and $mobile_number_error = "" and $email_address_error = "" and $username_error = "" and $password_error = "" and $confirm_password_error = "") { }
为此:
if($first_name_error == "" && $last_name_error == "" && $mobile_number_error == "" && $email_address_error == "" && $username_error == "" && $password_error == "" && $confirm_password_error == "") { }
不要使用sha1()之类的函数散列密码。。。使用PHP的
密码\u散列
。您不需要定义$mobile\u number\u error
。如果你打开错误报告,你会发现它是未定义的。非常感谢
if($first_name_error == "" && $last_name_error == "" && $mobile_number_error == "" && $email_address_error == "" && $username_error == "" && $password_error == "" && $confirm_password_error == "") { }