Skip to main content

How to Create a Simple Birthday Selector and Save to MySQL Database in PHP

Screenshot
Body
In this tutorial, I'm going to show you how to create a simple birthday day selector using PHP. This tutorial will now give you a good design but will give you idea on the topic. Also, if you want, you may learn Date Conversions.

Creating our Database

First, we're going to create our database. This contains the location of the dates that we are going to add. 1. Open phpMyAdmin. 2. Click databases, create a database and name it as "select_date". 3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.
  1. CREATE TABLE `birthday` (
  2. `birthdayid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `your_name` VARCHAR(30) NOT NULL,
  4. `birth_date` DATE NOT NULL,
  5. PRIMARY KEY(`birthdayid`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
date_select

Creating our Connection

Next step is to create a database connection and save it as "conn.php". This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.
  1. <?php
  2.  
  3. //MySQLi Procedural
  4. $conn = mysqli_connect("localhost","root","","select_date");
  5. if (!$conn) {
  6. die("Connection failed: " . mysqli_connect_error());
  7. }
  8.  
  9. ?>

Creating our Form and Sample Table

Lastly, We create our add birthday form and our sample table. We name this as "index.php".
  1. <?php include('conn.php'); ?>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Simple Birthday Selector and Save to MySQL Database using PHP/MySQLi</title>
  6. </head>
  7. <body>
  8. <form method="POST">
  9. <h2>Birthday Form</h2>
  10. Name: <input type="text" name="your_name" required><br><br>
  11. Birthday:
  12. <select name="month">
  13. <option value="0">Select Month</option>
  14. <?php
  15. for( $m = 1; $m <= 12; $m++ ) {
  16. $num = str_pad( $m, 2, 0, STR_PAD_LEFT );
  17. $month = date( 'F', mktime( 0, 0, 0, $m + 1, 0, 0, 0 ) );
  18. //if the above code won't work, you may try this:
  19. //$month = date("F", mktime(0, 0, 0, $m, 1));
  20. ?>
  21. <option value="<?php echo $num; ?>"><?php echo $month; ?></option>
  22. <?php
  23. }
  24. ?>
  25. </select>
  26. <select name="day">
  27. <option value="0">Select Day</option>
  28. <?php
  29. for( $a = 1; $a <= 31; $a++ ) {
  30. ?>
  31. <option value="<?php echo $a; ?>"><?php echo $a; ?></option>
  32. <?php
  33. }
  34. ?>
  35. </select>
  36. <select name="year">
  37. <option value="0">Select Year</option>
  38. <?php
  39. for( $y = 1990; $y <= 2100; $y++ ) {
  40. ?>
  41. <option value="<?php echo $y; ?>"><?php echo $y; ?></option>
  42. <?php
  43. }
  44. ?>
  45. </select>
  46. <br><br>
  47. <input type="submit" value="Submit" name="add_birthday">
  48. </form><br>
  49. <?php
  50. if (isset($_POST['add_birthday'])){
  51.  
  52. if ($_POST['day']==0 or $_POST['month']==0 or $_POST['year']==0){
  53. echo "Please Complete the Birthday Selection";
  54. }
  55. else{
  56. $name=$_POST['your_name'];
  57. $m=$_POST['month'];
  58. $d=$_POST['day'];
  59. $y=$_POST['year'];
  60. $date=$y.'-'.$m.'-'.$d;
  61.  
  62. echo 'You have selected: '.$date;
  63.  
  64. mysqli_query($conn,"insert into birthday (your_name, birth_date) values ('$name', '$date')");
  65. }
  66. }
  67. ?>
  68. <h2>Our Birthday Table</h2>
  69. <table border="1">
  70. <thead>
  71. <th>Name</th>
  72. <th>Birthday</th>
  73. </thead>
  74. <tbody>
  75. <?php
  76. $query=mysqli_query($conn,"select * from `birthday`");
  77. while($row=mysqli_fetch_array($query)){
  78. ?>
  79. <tr>
  80. <td><?php echo $row['your_name']; ?></td>
  81. <td><?php echo date('F d, Y', strtotime($row['birth_date'])); ?></td>
  82. </tr>
  83. <?php
  84. }
  85. ?>
  86. </tbody>
  87. </table>
  88.  
  89. </body>
  90. </html>

Comments

Submitted byDigitalkrafter (not verified)on Mon, 09/04/2017 - 18:58

Hi sourcecodester very nice code But When i am using
  1. $month = date( 'F', mktime( 0, 0, 0, $m + 1, 0, 0, 0 ) );
it displays only January as month in multiple times instead of above code i am using below code
  1. $month = date("F", mktime(0, 0, 0, $m, 1));
please do the recheck your code on this Thank you

Add new comment