PHP - 代码运行成功但没有任何内容插入到DB中

时间:2018-03-12 11:48:42

标签: php mysql

我正在尝试将用户的出生日期插入到数据库中,我有以下代码

<?php
include("db_con.php");
$dsn = "mysql:host=localhost;dbname=user";
$username = "root"; 
$password = "";
$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$pdo = new PDO($dsn, $username, $password, $options);
$M_seconds = 15;
$M_tries = 3;

if(isset($_POST['register'])){
                $form = $_POST;
                $username = $form[ 'username' ];
                $password = $form[ 'password' ];
                $email = $form[ 'email' ];
                $date_of_birth = $form['year'] . '-' . $form['month'] . '-' . $form['day'];

                //add min_length to the password
                if(strlen($password) < 6){
                echo 'ERROR: password is too short';
                }else{
                //Retrieve the field values from our registration form.
                $username = !empty($_POST['username']) ? trim($_POST['username']) : null;
                $pass = !empty($_POST['password']) ? trim($_POST['password']) : null;

                //Construct the SQL statement and prepare it.
                $sql = "SELECT COUNT(username) AS num FROM user_details WHERE username = :username";
                $stmt = $db->prepare($sql);

                //Bind the provided username to our prepared statement.
                $stmt->bindValue(':username', $username);

                //Execute.
                $stmt->execute();

                //Fetch the row.
                $row = $stmt->fetch(PDO::FETCH_ASSOC);


                if($row['num'] > 0){
                    echo 'That username already exists!';
                }else{

                //Hash the password for security.
                $passwordHash = password_hash($pass, PASSWORD_DEFAULT);
                $passwordHash = substr( $passwordHash, 0, 60 );

                //Prepare our INSERT statement.
                //Remember: We are inserting a new row into  table.
                $sql = "INSERT INTO user_details (username, password, email, date_of_birth) VALUES (:username, :password, :email, :date_of_birth)";
                $stmt = $db->prepare($sql);

                //Bind our variables.
                $stmt->bindValue(':username', $username);
                $stmt->bindValue(':password', $passwordHash);
                $stmt->bindValue(':email', $email);
                $stmt->bindValue(':date_of_birth', $date_of_birth);

                //Execute the statement and insert the new account.
                $result = $stmt->execute();

                //If the signup process is successful.
                if($result){
                    //What you do here is up to you!
                    echo 'Thank you for registering with our website.';
                }
                }

            }
}
?>

此代码用于插入用户名和密码,但是一旦我添加了出生日期和我收到的电子邮件echo 'Thank you for registering with our website.';,这意味着注册过程已经运行,但我的数据库中没有任何内容没有用户名,密码,电子邮件或出生日期。 FORM

<form method="post">
  <table class="loginTable">
     <tr>
      <th>ADMIN PANEL LOGIN</th>
     </tr>
     <tr>
      <td>
        <label class="firstLabel">Username:</label>
        <input type="text" name="username" id="username" value="" autocomplete="off" />
      </td>
     </tr>
     <tr>
      <td><label>Password:</label>
        <input type="password" name="password" id="password" value="" autocomplete="off" /></td>
     </tr>
     <tr>
      <td><label>Email:</label>
        <input type="text" name="email" id="email" value="" autocomplete="off" /></td>
     </tr>
     <select name='year'>
        <option>2012</option>
        <option>2011</option>
        <option>2010</option>
        <option>2009</option>
        <option>2008</option>
        <option>2007</option>
        <option>2006</option>
        <option>2005</option>
        <option>2004</option>
        <option>2003</option>
        <option>2002</option>
        <option>2001</option>
        <option>2000</option>
        <option>1999</option>
        <option>1998</option>
        <option>1997</option>
        <option>1996</option>
        <option>1995</option>
        <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>
        </select>
        <select name='month'>
        <option>01</option>
        <option>02</option>
        <option>03</option>
        <option>04</option>
        <option>05</option>
        <option>06</option>
        <option>07</option>
        <option>08</option>
        <option>09</option>
        <option>10</option>
        <option>11</option>
        <option>12</option>
        </select>
        <select name='day'>
        <option>01</option>
        <option>02</option>
        <option>03</option>
        <option>04</option>
        <option>05</option>
        <option>06</option>
        <option>07</option>
        <option>08</option>
        <option>09</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>



     <tr>
      <td>
         <input type="submit" name="register" id="register" value="Login" />
         <span class="loginMsg"><?php echo @$msg;?></span>
      </td>
     </tr>
  </table>
</form>

数据库结构

CREATE TABLE `user_details` (
  `id` int(11) NOT NULL,
  `username` varchar(50) NOT NULL,
  `password` varchar(255) NOT NULL,
  `email` varchar(100) NOT NULL,
  `date_of_birth` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

3 个答案:

答案 0 :(得分:1)

删除(:)

再试一次
$stmt->bindValue('username', $username);
$stmt->bindValue('password', $passwordHash);
$stmt->bindValue('email', $email);
$stmt->bindValue('date_of_birth', $date_of_birth);

此外,date_of_birth数据类型应该(日期)更好。

答案 1 :(得分:0)

id int(11)NOT NULL,

如果id列是自动增量,则插入

时无需给出值

如果不是自动增量,则应该给出id值

try {

$sql = "INSERT INTO user_details (username, password, email, date_of_birth) VALUES (:username, :password, :email, :date_of_birth)";
                $stmt = $db->prepare($sql);


 $stmt->bindValue(':username', $username);
                $stmt->bindValue(':password', $passwordHash);
                $stmt->bindValue(':email', $email);
                $stmt->bindValue(':date_of_birth', $date_of_birth);

 $stmt->execute();

    }
catch(PDOException $e)
    {
    echo "Error: " . $e->getMessage();
    }

嗨,你可以检查试试它会给......恐怖

答案 2 :(得分:0)

我已将选项框更改为使用type=date提交的输入,现在一切正常

<td><label>Date of Birth:</label>
        <input type="date" name="date_of_birth" id="date_of_birth" value="" autocomplete="off" /></td>
     </tr>