由于表单验证,我的代码未插入数据库

时间:2019-02-10 20:43:51

标签: 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:</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;
 }
?>

我意识到我的代码存在来自的错误

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 = "")

请,我该如何修改?除了上述功能之外,我所有的功能和其他所有代码都可以正常工作。

2 个答案:

答案 0 :(得分:2)

首先,您应该在此处使用“ ==”,而不是“ =”,而“ &&”而不是“ and”。

即改变这个:

    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 == "") { }

答案 1 :(得分:0)

确保您定义了$ mobile_number_error并尝试再次执行代码并查看。