首页 > 解决方案 > SQLSTATE [23000]:违反完整性约束:1048 列“密码”不能为空

问题描述

我在以前的托管中一直在使用此代码,并且运行良好,但是自从我迁移到新的托管服务提供商后,我收到了以下错误消息:SQLSTATE [23000]:完整性约束违规:1048 列“密码”不能为空.

请有人帮助我,我真的很想将此脚本用于我正在处理的项目,谢谢

这是我的表格

<form role="form" method="post" action="">
                
                <hr>

                <?php
                //check for any errors
                if(isset($error)){
                    foreach($error as $error){
                        echo '<p class="bg-danger">'.$error.'</p>';
                    }
                }

                //if action is joined show sucess
                if(isset($_GET['action']) && $_GET['action'] == 'joined'){
                    echo "<h2 class='bg-success'>Registration successful, please check your email to activate your account.</h2>";
                }
                ?>

                <table class="table">
    <tr>
    <th scope="row"><label for="first_name">First Name</label></th>
    <input class="input" type="hidden" name="show_code" value="transfer/cot-.php"/>
    
   <td><input class="input" type="text" pattern="[a-zA-Z0-9]{2,64}" name="first_name" required /></td></tr>
   <tr>
    <th scope="row"><label for="last_name">Last Name:</label></th>
   <td><input class="input" type="text" pattern="[a-zA-Z0-9]{2,64}" name="last_name" required /></td></tr>
   
   <tr>
   <th scope="row"><label for="user_name">User Name:</label></th>
    <td><input class="input" type="text" pattern="[a-zA-Z0-9 ]{2,64}" name="user_name" value="<?php if(isset($error)){ echo htmlspecialchars($_POST['user_name'], ENT_QUOTES); } ?>" required /></td></tr>
    <tr>
   <th scope="row"><label for="user_name">Account Name:</label></th>
    <td><input class="input" type="text" pattern="[a-zA-Z0-9 ]{2,64}" name="account_name" required /></td></tr>
    
    
<th scope="row">Account Type:</th>
   <td><select name="account_type" style="width:150px;">
  <option value="Current">Current</option>
    <option value="Savings">Savings</option>
    <option value="Recurring Deposit">Recurring Deposit</option>
    <option value="Fixed Deposit">Fixed Deposit</option>
    
  </select></td></tr>
  
    <tr>
    <th scope="row"><label for="user_email">Email, <br>(please provide a real email address, <br>you'll get a verification mail with an activation link)</label></th>
    <td><input class="input" type="email" name="email" value="<?php if(isset($error)){ echo htmlspecialchars($_POST['email'], ENT_QUOTES); } ?>" required /></td></tr>

    <tr>
    <th scope="row"><label for="password">Password (min. 6 characters!)</label></th>
    <td><input class="input" type="password" name="password" pattern=".{6,}" required autocomplete="off" /></td></tr>

    <tr>
    <th scope="row"><label for="passwordConfirm">Password repeat</label></th>
    <td><input class="input" type="password" name="passwordConfirm" pattern=".{6,}" required autocomplete="off" /></td></tr>

   <tr>
   <th scope="row">Gender:</th>
   <td><select name="gender" style="width:150px;">
  <option value="Male">Male</option>
    <option value="Female">Female</option>
  </select></td></tr>
  <tr>
   <th scope="row">Date of Birth:</th>
   <td> <select name="day">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
    <option>11</option>
    <option>12</option>
    <option>13</option> 
    <option>14</option> 
    <option>15</option> 
    <option>16</option> 
    <option>17</option> 
    <option>18</option> 
    <option>19</option> 
    <option>20</option> 
    <option>21</option> 
    <option>22</option> 
    <option>23</option> 
    <option>24</option> 
    <option>25</option> 
    <option>26</option> 
    <option>27</option> 
    <option>28</option> 
    <option>29</option> 
    <option>30</option> 
    <option>31</option> 
  </select>
  /
  
  <select name="month">
    <option>January</option>
    <option>February</option>
    <option>March</option>
    <option>April</option>
    <option>May</option>
    <option>June</option>
    <option>July</option>
    <option>August</option>
    <option>September</option>
    <option>October</option>
    <option>November </option>
    <option>December </option> 
  </select>/
  
  
  <select name="year">
  <option>1994</option>
  <option>1993</option>
  <option>1992</option>
  <option>1991</option>
  <option>1990</option>
  <option>1989</option>
  <option>1988</option>
  <option>1987</option>
  <option>1986</option>
  <option>1985</option>
  <option>1984</option>
  <option>1983</option>
  <option>1982</option>
  <option>1981</option>
  <option>1980</option>
  <option>1979</option>
  <option>1978</option>
  <option>1977</option>
  <option>1976</option>
  <option>1975</option>
  <option>1974</option>
  <option>1973</option>
  <option>1972</option>
  <option>1971</option>
  <option>1970</option>
  <option>1969</option>
  <option>1968</option>
  <option>1967</option>
  <option>1966</option>
  <option>1965</option>
  <option>1964</option>
  <option>1963</option>
  <option>1962</option>
  <option>1961</option>
  <option>1960</option>
  <option>1959</option>
  <option>1958</option>
  <option>1957</option>
  <option>1956</option>
  <option>1955</option>
  <option>1954</option>
  <option>1953</option>
  <option>1952</option>
  <option>1951</option>
  <option>1950</option>
  <option>1949</option>
  <option>1948</option>
  <option>1947</option>
  <option>1946</option>
  <option>1945</option>
  <option>1944</option>
  <option>1943</option>
  <option>1942</option>
  <option>1941</option>
  <option>1940</option>
  
  
  
  </select></td>
   </tr>
   
   <tr>
   <th scope="row">Occupation:</th>
   <td><input class="input" type="text" name="occupation" required /></td></tr>
   
    <tr>
   <th scope="row">Home Address:</th>
   <td><input class="input" type="text" name="home_address" required /></td></tr>
   <tr>
   <th scope="row">Phone Number:</th>
   <td><input class="input" type="text" name="phone_no" required /></td>
   </tr>
  </table>
  
<br>
<table>
<tr><td>

<input type="checkbox" name="checkbox1" id="checkbox1" required /> I have read and accept your terms and conditions</td></tr>
</table>
 
  
   
    <input type="submit" name="submit" value="Register" />
    </table>
            </form>

这是我的控制器

<?php require('includes/config.php');

//if logged in redirect to members page
if( $user->is_logged_in() ){ header('Location: account.php'); exit(); }

//if form has been submitted process it
if(isset($_POST['submit'])){
    if (!isset($_POST['first_name'])) $error[] = "Please fill out all fields";
    if (!isset($_POST['last_name'])) $error[] = "Please fill out all fields";
    if (!isset($_POST['user_name'])) $error[] = "Please fill out all fields";
    if (!isset($_POST['account_name'])) $error[] = "Please fill out all fields";
    if (!isset($_POST['account_type'])) $error[] = "Please fill out all fields"; 
    if (!isset($_POST['email'])) $error[] = "Please fill out all fields";
    if (!isset($_POST['password'])) $error[] = "Please fill out all fields";
    if (!isset($_POST['gender'])) $error[] = "Please fill out all fields";
    if (!isset($_POST['occupation'])) $error[] = "Please fill out all fields";
    if (!isset($_POST['home_address'])) $error[] = "Please fill out all fields";
    if (!isset($_POST['phone_no'])) $error[] = "Please fill out all fields";
    if (!isset($_POST['show_code'])) $error[] = "Please fill out all fields";

    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $user_name = $_POST['user_name'];
    $account_name = $_POST['account_name'];
    $account_type = $_POST['account_type'];
    $gender = $_POST['gender'];
    $occupation = $_POST['occupation'];
    $home_address = $_POST['home_address'];
    $phone_no = $_POST['phone_no'];
    $show_code = $_POST['show_code'];

    //very basic validation
    if(!$user->isValidUsername($user_name)){
        $error[] = 'Usernames must be at least 3 Alphanumeric characters';
    } else {
        $stmt = $db->prepare('SELECT user_name FROM users WHERE user_name = :user_name');
        $stmt->execute(array(':user_name' => $user_name));
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if(!empty($row['user_name'])){
            $error[] = 'Username provided is already in use.';
        }

    }

    if(strlen($_POST['password']) < 3){
        $error[] = 'Password is too short.';
    }

    if(strlen($_POST['passwordConfirm']) < 3){
        $error[] = 'Confirm password is too short.';
    }

    if($_POST['password'] != $_POST['passwordConfirm']){
        $error[] = 'Passwords do not match.';
    }

    //email validation
    $email = htmlspecialchars_decode($_POST['email'], ENT_QUOTES);
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
        $error[] = 'Please enter a valid email address';
    } else {
        $stmt = $db->prepare('SELECT email FROM users WHERE email = :email');
        $stmt->execute(array(':email' => $email));
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if(!empty($row['email'])){
            $error[] = 'Email provided is already in use.';
        }

    }


    //if no errors have been created carry on
    if(!isset($error)){

        //hash the password
        $hashedpassword = $user->password_hash($_POST['password'], PASSWORD_BCRYPT);

        //create the activasion code
        $activasion = md5(uniqid(rand(),true));

        try {

            //insert into database with a prepared statement
            $stmt = $db->prepare('INSERT INTO users (first_name,last_name,user_name,account_name,account_type,email,password,gender,occupation,home_address,phone_no,show_code,active) VALUES (:first_name, :last_name, :user_name, :account_name, :account_type, :email, :password, :gender, :occupation, :home_address, :phone_no, :show_code, :active)');
            $stmt->execute(array(
                ':first_name' => $first_name,
                ':last_name' => $last_name,
                ':user_name' => $user_name,
                ':account_name' => $account_name,
                ':account_type' => $account_type,
                ':email' => $email,
                ':password' => $hashedpassword,
                ':gender' => $gender,
                ':occupation' => $occupation,
                ':home_address' => $home_address,
                ':phone_no' => $phone_no,
                ':show_code' => $show_code,
                ':active' => $activasion
            ));
            $id = $db->lastInsertId('user_id');

            //send email
            $to = $_POST['email'];
            $subject = "Registration Confirmation";
            $body = "<p>Thank you for registering with us.</p>
            <p>To activate your account, please click on this link: <a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a></p>
            <p>Regards</p>";

            $mail = new Mail();
            $mail->setFrom(SITEEMAIL);
            $mail->addAddress($to);
            $mail->subject($subject);
            $mail->body($body);
            $mail->send();

            //redirect to index page
            header('Location: register.php?action=joined');
            exit;

        //else catch the exception and show the error.
        } catch(PDOException $e) {
            $error[] = $e->getMessage();
        }

    }

}


?>

标签: phphtml

解决方案


推荐阅读