0

I've looked up how to do this but I just can't seem to get it right. Please tell me how to combine the month, day, and year, into one date to be inserted into the mysql database. My problem is not actually sending information to the database. My problem is I'm sending 3 separate fields (month, day, year) to the database. Please instruct me exactly where to put your recommended codes. Here's my code:

<?php
function register_user($register_data) {
array_walk($register_data, 'array_sanitize');

$fields = '`' . implode('`, `', array_keys($register_data)) . '`';
$data = '\'' . implode('\', \'', $register_data) . '\'';

mysql_query("INSER INTO `users` ($fields) VALUES ($data)");
}
?>

<form action="" method="post">
    <ul>
        <li>
            <select name="month">
                <option value="01">January</option>
                //all the other month options
            </select>
            <select name="day">
                <option value="01">1</option>
                //all the other days
            </select
            <select name="year">
                <option value="2013">2013</option>
                //more year options
            </select>
        </li>    
        <li>
    <input type="submit" name="registrationform" value="Sign up">
    </li>
    </ul>
</form>

<?php
if (empty($_POST) === false && empty($errors) === true){
    $register_data = array(
        'Month'     => $_POST['month'],
        'Day'       => $_POST['day'],
        'Year'      => $_POST['year']
    );

    register_user($register_data);
    //redirect
    exit();
?>
ST3
  • 8,826
  • 3
  • 68
  • 92
Kacy Raye
  • 1,312
  • 1
  • 11
  • 14
  • 4
    I'd suggest you look into MySQL Documentation on [DATE](http://dev.mysql.com/doc/refman/5.1/en/date-and-time-types.html) – silentw Jan 07 '13 at 17:48

5 Answers5

0

Make your birthday column a DATE or DATETIME field in MySQL, then insert it like so:

$_POST['year'] . "-" . $_POST['month'] . "-" . $_POST['day']
Matt Dodge
  • 10,833
  • 7
  • 38
  • 58
0

You take the year, month, and then day (in that order) and between each you place a hyphen (ASCII character # 45). And that is it.

Take care you have 4 numbers for year, two numbers for month and two numbers for day (also digits called are these numbers).

It is important that your outcome looks like in the following examples:

October 3rd 2011    :=    2011-10-03
1st Feb '13         :=    2013-02-01

Also take care you don't have some date that does not exists, like 31. Of November or something similar.

And:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
hakre
  • 193,403
  • 52
  • 435
  • 836
0

I would concatenate the birthday field like so:

$birthday = $_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['day'];

Then you can insert it:

mysql_query("INSERT INTO users (birthday) VALUES ('" . $birthday . "')");
SeanWM
  • 16,789
  • 7
  • 51
  • 83
0

You have many ways ..

You can split them to three different fields in the database of type int ..

OR

You can use the field as date

CREATE TABLE `date` (
  `dp_date` date NOT NULL default '0000-00-00',
) ;

$query_manual = "INSERT INTO date(dp_date)
VALUES ($_POST['year']-$_POST['month']-$_POST['day']')";
sikas
  • 5,435
  • 28
  • 75
  • 120
0
<?php
function register_user($register_data) {
$register_data['birthday'] = $register_data['year'] . '-' . $register_data['month'] . '-' . $register_data['day'];
unset($register_data['year']);
unset($register_data['month']);
unset($register_data['day']);
array_walk($register_data, 'array_sanitize');

$fields = '`' . implode('`, `', array_keys($register_data)) . '`';
$data = '\'' . implode('\', \'', $register_data) . '\'';

mysql_query("INSER INTO `users` ($fields) VALUES ($data)");
}
?>

you will then need a column in your table called 'birthday' and it should be of the type 'DATE'

pebbo
  • 581
  • 5
  • 15