简体   繁体   中英

How to INSERT multiple HTML form fields into a single MySQL column as date

Problem:

I'm having trouble finding the documentation or both relevant and answered answers to my situation.

Basically, I'm wanting to generate a MySQL-ready date ready for insertion by using multiple html form dropdown fields.

Currently my insertion code uses a single field for the date, which I enter manually in a text field in MySQL format:

$sql=
    " INSERT INTO events (e_date, e_time, e_ampm, e_type, e_name) " .
    " VALUES ('{$form_e_date}','{$form_e_time}', '{$form_e_ampm}', '{$form_e_type}','{$form_e_name}')";

Instead of $form_e_date (which I currently enter in text field using format YYYY-MM-DD), I want to use 3 separate form fields (such as $month, $day, $year) with values selected from dropdown select boxes.

Question:

Is there a way to concatentate 3 variables ($year, $month, $day, delineated with "-" for MySQL) in place of the $form_e_date, and if so, how would you structure this syntax?

I'm also open to approaching this a better way (still using MySQL/PHP); I just need help or direction towards the resources.

Any help is greatly appreciated!

Additional info:

PS The current syntax is

VALUES ('{$form_e_date}','{$form_e_time}', ... etc.

instead of

VALUES ('$_POST[form_e_date]','$_POST[form_e_time]', ... etc.

as part of a PHP fix to Undefined index notices I had been getting.

Also, the solution

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

is not preferable to me as it is since it causes Undefined index notices on the new year, month, and day variables, even when they are defined beforehand using a technique that otherwise eliminates Undefined index notices, for example:

if (array_key_exists("year", $_POST))
                $year = mysql_real_escape_string($_POST["year"]);
            else
                $year = null;

Code #abc (for comment to JT Smith's answer):

$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];

$whole_date = array($year, $month, $day);
$mysql_formatted_date = implode("-", $whole_date);


$sql=
" INSERT INTO events (e_date, e_time, e_ampm, e_type, e_name) " .
" VALUES ('{$mysql_formatted_date}','{$form_e_time}', '{$form_e_ampm}', '{$form_e_type}','{$form_e_name}')";

Code #fgh:

<select name="month">
    <option value="01">Jan</option>
<option value="02">Feb</option>
<option value="03">Mar</option>
<option value="04">Apr</option>
<option value="05">May</option>
<option value="06">Jun</option>
<option value="07">Jul</option>
<option value="08">Aug</option>
<option value="09">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>

<select name="day">
    <?php
    for ($i = 1; $i <=31; $i++) {
        echo "<option value=\"$i\">$i</option>";
    }
?>
</select>

<select name="year">
    <option value="2012">2012</option>
    <option value="2013">2013</option>
</select>

根据您的要求在php中连接它们,也可以使用mysql concat函数

Why do you need to concat it within the mysql query? Why not just concat it into a single variable and use that variable (as you do now) into the query?

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

$query = " INSERT INTO events (e_date, e_time, e_ampm, e_type, e_name) " .
" VALUES ('{$new_date}','{$form_e_time}', '{$form_e_ampm}', '{$form_e_type}','{$form_e_name}')";

Concatenation operator in PHP is a dot . Syntax would look like:

$date = $year . '-' . $month . '-' . $day;

Anyway a better approach would be to store the date/time in a single db field of type datetime. Check documentation for date() function and available formats.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM