• We just launched and are currently in beta. Join us as we build and grow the community.

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

crackor

World Builder
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
110
Likes
33
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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;

database_6_1.png

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. //MySQLi Procedural
  3. $conn

    =

    mysqli_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "select_date"

    )

    ;
  4. if

    (

    !

    $conn

    )

    {
  5. die

    (

    "Connection failed: "

    .

    mysqli_connect_error

    (

    )

    )

    ;
  6. }

  7. ?>

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. if

    (

    $_POST

    [

    'day'

    ]

    ==

    0

    or $_POST

    [

    'month'

    ]

    ==

    0

    or $_POST

    [

    'year'

    ]

    ==

    0

    )

    {
  52. echo

    "Please Complete the Birthday Selection"

    ;
  53. }
  54. else

    {
  55. $name

    =

    $_POST

    [

    'your_name'

    ]

    ;
  56. $m

    =

    $_POST

    [

    'month'

    ]

    ;
  57. $d

    =

    $_POST

    [

    'day'

    ]

    ;
  58. $y

    =

    $_POST

    [

    'year'

    ]

    ;
  59. $date

    =

    $y

    .

    '-'

    .

    $m

    .

    '-'

    .

    $d

    ;

  60. echo

    'You have selected: '

    .

    $date

    ;

  61. mysqli_query

    (

    $conn

    ,

    "insert into birthday (your_name, birth_date) values ('$name

    ', '$date

    ')"

    )

    ;
  62. }
  63. }
  64. ?>
  65. <h2>Our Birthday Table</h2>
  66. <table border="1">
  67. <thead>
  68. <th>Name</th>
  69. <th>Birthday</th>
  70. </thead>
  71. <tbody>
  72. <?php
  73. $query

    =

    mysqli_query

    (

    $conn

    ,

    "select * from `birthday`"

    )

    ;
  74. while

    (

    $row

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  75. ?>
  76. <tr>
  77. <td><?php

    echo

    $row

    [

    'your_name'

    ]

    ;

    ?>

    </td>
  78. <td><?php

    echo

    date

    (

    'F d, Y'

    ,

    strtotime

    (

    $row

    [

    'birth_date'

    ]

    )

    )

    ;

    ?>

    </td>
  79. </tr>
  80. <?php
  81. }
  82. ?>
  83. </tbody>
  84. </table>

  85. </body>
  86. </html>


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

338,631

338,639

Top