0

我已经查找了如何做到这一点,但我似乎无法做到这一点。请告诉我如何将月、日和年组合成一个日期以插入 mysql 数据库。我的问题实际上并不是将信息发送到数据库。我的问题是我将 3 个单独的字段(月、日、年)发送到数据库。请告诉我在哪里放置您推荐的代码。这是我的代码:

<?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();
?>
4

5 回答 5

0

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

$_POST['year'] . "-" . $_POST['month'] . "-" . $_POST['day']
于 2013-01-07T17:50:52.897 回答
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.

于 2013-01-07T17:51:22.117 回答
0

我会像这样连接生日字段:

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

然后你可以插入它:

mysql_query("INSERT INTO users (birthday) VALUES ('" . $birthday . "')");
于 2013-01-07T17:51:43.803 回答
0

你有很多方法..

int您可以将它们拆分为类型为..的数据库中的三个不同字段。

或者

您可以将该字段用作日期

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']')";
于 2013-01-07T17:52:27.550 回答
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'

于 2013-01-07T17:55:04.303 回答