Site icon T4Tutorials.com

Get Date of Birth and Store it in Database using PHP

Get Date of Birth and Store it in Database using PHP

To get the date of birth, we are going to use an HTML form. And we will be using PHP to store it in the database.

Database of getting the Date of Birth and Store it in Database using PHP

Code of getting the Date of Birth and Store it in Database using PHP

<!DOCTYPE html>
<html>
<head>
   <title></title>
</head>
<body>
<form method="POST" action="">
<select name="Year">
  <option value="">Select year</option>
  <?php for ($i = 1980; $i < date('Y'); $i++) : ?>
  <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
  <?php endfor; ?>
</select>
<select name="Month">
  <option value="">Select month</option>
  <?php for ($i = 1; $i <= 12; $i++) : ?>
  <option value="<?php echo ($i < 10) ? '0'.$i : $i; ?>"><?php echo $i; ?></option>
  <?php endfor; ?>
</select>
<select name="Date">
  <option value="">Select date</option>
  <?php for ($i = 1; $i <= 31; $i++) : ?>
  <option value="<?php echo ($i < 10) ? '0'.$i : $i; ?>"><?php echo $i; ?></option>
  <?php endfor; ?>
</select>
   <input type="submit" name="sub">
</form>
</body>
</html>
<?php
$conn = mysqli_connect("localhost","root","","getdob");
if(isset($_POST['sub']))
{
$Year = $_POST['Year'];
$Month = $_POST['Month'];
$Date = $_POST['Date'];
if ($Year != '' && $Month != '' && $Date != '') 
   {
   $date = $Year.'-'.$Month.'-'.$Date;
   $sql="INSERT INTO dob VALUES  (Null,'$date')";
   if (mysqli_query($conn,$sql))
   {
      echo "Record added!";
   }
   else
   {
      echo "Error!!!";
   }
   }
else
   {
      echo "Please Select Day, Month and Year!!!";
   }   
}
?>

Output:

Download Zip – GetDOBusingPHP

 

Exit mobile version