crackor
World Builder
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.
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.
Creating our Form and Sample Table
Lastly, We create our add birthday form and our sample table. We name this as "index.php".
Download
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.
- CREATE
TABLE
`birthday`
(
- `birthdayid`
INT
(
11
)
NOT
NULL
AUTO_INCREMENT
,
- `your_name`
VARCHAR
(
30
)
NOT
NULL
,
- `birth_date`
DATE
NOT
NULL
,
- PRIMARY
KEY
(
`birthdayid`
)
- )
ENGINE=
InnoDB DEFAULT
CHARSET=
latin1;

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.
- <?php
- //MySQLi Procedural
- $conn
=
mysqli_connect
(
"localhost"
,
"root"
,
""
,
"select_date"
)
;
- if
(
!
$conn
)
{
- die
(
"Connection failed: "
.
mysqli_connect_error
(
)
)
;
- }
- ?>
Creating our Form and Sample Table
Lastly, We create our add birthday form and our sample table. We name this as "index.php".
- <?php
include
(
'conn.php'
)
;
?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Simple Birthday Selector and Save to MySQL Database using PHP/MySQLi</title>
- </head>
- <body>
- <form method="POST">
- <h2>Birthday Form</h2>
- Name: <input type="text" name="your_name" required><br><br>
- Birthday:
- <select name="month">
- <option value="0">Select Month</option>
- <?php
- for
(
$m
=
1
;
$m
<=
12
;
$m
++
)
{
- $num
=
str_pad
(
$m
,
2
,
0
,
STR_PAD_LEFT )
;
- $month
=
date
(
'F'
,
mktime
(
0
,
0
,
0
,
$m
+
1
,
0
,
0
,
0
)
)
;
- //if the above code won't work, you may try this:
- //$month = date("F", mktime(0, 0, 0, $m, 1));
- ?>
- <option value="<?php
echo
$num
;
?>
"><?php
echo
$month
;
?>
</option>
- <?php
- }
- ?>
- </select>
- <select name="day">
- <option value="0">Select Day</option>
- <?php
- for
(
$a
=
1
;
$a
<=
31
;
$a
++
)
{
- ?>
- <option value="<?php
echo
$a
;
?>
"><?php
echo
$a
;
?>
</option>
- <?php
- }
- ?>
- </select>
- <select name="year">
- <option value="0">Select Year</option>
- <?php
- for
(
$y
=
1990
;
$y
<=
2100
;
$y
++
)
{
- ?>
- <option value="<?php
echo
$y
;
?>
"><?php
echo
$y
;
?>
</option>
- <?php
- }
- ?>
- </select>
- <br><br>
- <input type="submit" value="Submit" name="add_birthday">
- </form><br>
- <?php
- if
(
isset
(
$_POST
[
'add_birthday'
]
)
)
{
- if
(
$_POST
[
'day'
]
==
0
or $_POST
[
'month'
]
==
0
or $_POST
[
'year'
]
==
0
)
{
- echo
"Please Complete the Birthday Selection"
;
- }
- else
{
- $name
=
$_POST
[
'your_name'
]
;
- $m
=
$_POST
[
'month'
]
;
- $d
=
$_POST
[
'day'
]
;
- $y
=
$_POST
[
'year'
]
;
- $date
=
$y
.
'-'
.
$m
.
'-'
.
$d
;
- echo
'You have selected: '
.
$date
;
- mysqli_query
(
$conn
,
"insert into birthday (your_name, birth_date) values ('$name
', '$date
')"
)
;
- }
- }
- ?>
- <h2>Our Birthday Table</h2>
- <table border="1">
- <thead>
- <th>Name</th>
- <th>Birthday</th>
- </thead>
- <tbody>
- <?php
- $query
=
mysqli_query
(
$conn
,
"select * from `birthday`"
)
;
- while
(
$row
=
mysqli_fetch_array
(
$query
)
)
{
- ?>
- <tr>
- <td><?php
echo
$row
[
'your_name'
]
;
?>
</td>
- <td><?php
echo
date
(
'F d, Y'
,
strtotime
(
$row
[
'birth_date'
]
)
)
;
?>
</td>
- </tr>
- <?php
- }
- ?>
- </tbody>
- </table>
- </body>
- </html>
Download
You must upgrade your account or reply in the thread to view the hidden content.