0

I am trying to insert date of birth of a user into a database i have the following code

<?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.';
                }
                }

            }
}
?>

This code have worked for insertion of username and password but once i added the date of birth and the email I am getting this echo 'Thank you for registering with our website.'; which means that the signup process have run but I am not getting anything in my database no username,password,email or date of birth. 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>

Database Structure

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;
5
  • Hi did you get any error? Commented Mar 12, 2018 at 11:54
  • @MathanKumar No i get no error Commented Mar 12, 2018 at 11:55
  • You aren't giving an ID for your user, the SQL you posted isn't making sure that your ID is an autoincrementing primary key, so you have to post the ID yourself. Commented Mar 12, 2018 at 11:55
  • Backtick your columns, password is reserved word in mysql. So INSERT INTO `user_details` (`username`, `password`, `email`, `date_of_birth`) VALUES (:username, :password, :email, :date_of_birth) Commented Mar 12, 2018 at 12:05
  • No errors? Are you sure? I can see that $db->prepare is being used without $db being set. Put the code in a try-catch block and see all the errors generated Commented Mar 12, 2018 at 12:46

3 Answers 3

2

Try again with removing (:)

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

Moreover, It is better that date_of_birth data type should (date).

Sign up to request clarification or add additional context in comments.

1 Comment

huh ? care to give an explanation ?
0

id int(11) NOT NULL,

if id column is auto increment no need to give value while inserting

If it is not auto increment you should give id value

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();
    }

Hi can you check try catch it will give..error

Comments

0

I have change the option box to a input filed with type=date now everything is working

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.