How to insert dates selected from drop downs into mySQL table using php?

When you have 5 dropdowns for date selection (Y, m,d, H,i) how do you insert the selected date and time into a single column of a mySQL table as a datetime using PHP5?

I know they need to be combined in a string such as:

$DateTime="$Year-$Month-$Day $Hour:$Minute:00";

and then maybe use strtotime:

$Date=date('Y-m-d H:i:s', strtotime($DateTime)) ;

but where do I declare the variables and how do I build it into a query such as:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "details")) { $insertSQL = sprintf("INSERT INTO sessions (Location1, Location2, Date) VALUES (%s, %s, $s)", GetSQLValueString($_POST['Location1'], "text"), GetSQLValueString($_POST['Location2'], "text"), GetSQLValueString($_POST[' ???? '], "date"));

-------------Problems Reply------------

You might also look into the mktime() function:

You would then just need to declare your date variable like:

$date = mktime($_POST['H'],$_POST['i'],0,$_POST['m'],$_POST['d'],$_POST['Y']);

$location1 = mysql_real_escape_string( $_POST['Location1'];
$location2 = mysql_real_escape_string( $_POST['Location2'];

$insertSQL = "INSERT INTO sessions (Location1, Location2, Date) VALUES ('$location1', '$location2', $date)";

mysql_query( $insertSQL );


Or, using the sprintf format you're using:

$date = mktime($_POST['H'],$_POST['i'],0,$_POST['m'],$_POST['d'],$_POST['Y']);

$insertSQL = sprintf("INSERT INTO sessions (Location1, Location2, Date)
VALUES (%s, %s, $s)",

GetSQLValueString($_POST['Location1'], "text"),
GetSQLValueString($_POST['Location2'], "text"),
GetSQLValueString($date, "date"));

I have never used the GetSQLValueString function before though, so I can only assume that's the correct way to use it.

Create a utility function that converts between the 'display mode' and MySQL mode, ti will come in handy in several other projects in the future.

Date - Return a string, formatted according to the given format string

MkTime - Get a Unix timestamp for a date

Strtotime - Parse English textual datetime description into a Unix timestamp

MySQL provides a function to make this easy for you.">FROM_UNIXTIME(). I would use mktime() to get your date to a timestamp, as it's overhead is much less than strtodate, since you know the date-parts there is no need to parse it as a string.

The following is the most straight-forward approach I can think of:

$date = mktime($_POST['H'], $_POST['i'], 0, $_POST['m'], $_POST['d'], $_POST['Y']);

$location1 = mysql_real_escape_string( $_POST['Location1'];
$location2 = mysql_real_escape_string( $_POST['Location2'];

$insertSQL = "INSERT INTO sessions (Location1, Location2, Date) VALUES ('$location1', '$location2', FROM_UNIXTIME($date))";

I'm assuming that GetSQLValueString is the Dreamweaver custom function (I've only heard of it). I'm sure you can adopt this into your implementation, though.

Thanks to all who tried to help - the answer was a lot less complicated (blonde moment I guess! lol) Just needed to declare the post variables as:

$_POST['startDate'] ="$Year-$Month-$Day $Hour:$Minute:00";

No need for time conversions as it's already in Mysql format (YEY!)

Category:php Views:2 Time:2009-04-10

Related post

  • I want to insert Date like 24-05-2010 in MySQL Database 2010-05-30

    I am using C# code for inserting record and datetimepicker control for Date I want to insert Date like 24-05-2010 in MySQL Database What Datatype i have to use for that in MySQL table and how to right insert query for to insert date? --------------So

  • inserting data selected by checkboxes 2012-05-03

    I am trying a code where I want to create a table and insert data into the table. I want to create table and insert data for only those fields which are selected by user using checkbox. This is my jsp code: <%@ page language="java" import="java.ut

  • How to bulk insert data from ref cursor to a temporary table in PL/SQL 2010-03-12

    Could anyone tell me how to bulk insert data from a ref cursor to a temporary table in PL/SQL? I have a procedure that one of its parameters stores a result set, this result set will be inserted to a temporary table in another stored procedure. This

  • Can't insert data from registration page (php) to mysql 2010-12-19

    Having trouble inserting data from .php file to mysql. Don't know what the problem is. I have two main .php files. The first is the homepage: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "">

  • Inserting data from html to php to mysql database 2011-12-20

    I have done my research but have found nothing specific enough to my problem I have an HTML form, asking for data, then a php script that is suppose to put the data in a mysql database When i try it on my localhost, i dont get any errors but when i c

  • How do I insert data from a Python dictionary to MySQL? 2010-04-12

    I manipulated some data from MySQL and the resulting dictionary "data" (print data) displays something like this : {'1': ['1', 'K', abc, 'xyz', None, None, None,, 6, 18)], '2': ['2', 'K', efg, 'xyz', None, None, None, None], '3': [

  • How do I select one row from a MySQL table? 2011-07-25

    If I know the query only returns one row, and more specefically, in this case one value, $full_name = database::query("SELECT concat (fname, ' ', lname) from cr WHERE email='$email'"); how do I get that value into a single variable. Database::query r

  • Is it possible to insert a new row at top of MySQL table? 2011-10-13

    All rows in MySQL tables are being inserted like this: 1 2 3 Is there any way how to insert new row at a top of table so that table looks like this? 3 2 1 Yes, yes, I know "order by" but let me explain the problem. I have a dating website and users c

  • load data local infile ,importing csv to mysql table not working properly 2011-10-14

    I am using load data infile to import data from csv to mysql table, but data is not inserted properly, in the csv file fields are enclosed by ", e.g. "field1","field2","field3" some fields consist of multiple lines.. here is my script.. load data loc

  • Insert object, struct or array into a MySQL table in C# 2011-12-11

    Is there any way to simplify data insertion into a MySQL table making use of objects, structs or maybe arrays instead of writing long SQL queries with dozen of fields listed? Say, I have a simple object of class (or struct) like this: public class Us

  • Selecting distinct values in a mysql table 2011-11-16

    Here's my mysql table: Table League (userid and lid are primary keys): *userid* *lid* rank win loss streak score --------------------------------------------------------- 2 1 1 2 0 2 10 2 3 2 1 1 1 5 5 1 2 1 1 1 5 I'm trying to select the users with

  • How to do not print default text "Print your date here" when insert date selection element 2014-02-22

    I tried to create document with fields. When adding field with date picker there is some default text - "Enter date here" (sory text may be different cause i have localized (not English version) of Word). At this time all is ok. But sometimes i do no

  • Which is the fastest way to insert data in PostgreSQL in a N:M table? 2011-10-31

    I have an application that need to insert a lot of rows in database, but the insertion is too slow at the moment, I want to know what are the best techniques to fast up the insertion to the maximum. The situation is in each insertion, I need to disco

  • Is it possible to insert data into the mid section of a table using the INSERT command? 2010-11-08

    Example: INDEX | SOME_DATA 1 | some_data1 3 | some_data3 4 | some_data4 5 | some_data5 I want to do an INSERT to add ---> 2 | some_data2. Is there an SQL command or some way of doing this? [edit] OK. Maybe if i explain every thing what i want to a

  • How to insert data from an EXECUTE statement in mySql? 2011-04-21

    I have data in a wp_users table, and I want to duplicate the data from that table (except for the ID column) into another table, called wp_users2. If I didn't care about the id column, which I want to auto-increment, I could just do this: insert into

  • Does Inserting Data Into SQL Server Lock the Whole Table 2011-09-13

    I am using the entity framework. I am inserting into our database records which include a blob field. The blob field can be up to 5MB of data. I was wondering when inserting a record into this table, does it lock the whole table? So if you are queryi

  • showing selections from drop downs in a table 2013-03-10

    i have set of drop downs with a select button below. what i want to try to do is once the user has been through the three drop downs and pressed select, the option that is chosen is shown in a corresponding table in its assigned category HTML: <fo

  • How to insert data in an many-to-many Association table using NHibernate 2009-11-05

    I have 2 tables in database. Formula and Ingredient. They have many-to-many relationship, so I have an association table in the db thats called FormulaIngredient. I am using and SQL server 2005. My FormulaIngredient table has ID, formulaID, in

  • How to select a date range of current month in mysql table? 2011-07-01

    I want to select current month rows in table I have tried this SELECT * FROM table WHERE YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) But it is not working. --------------Solutions------------- I'm not an expert on the MySQL engine

Copyright (C), All Rights Reserved.

processed in 0.083 (s). 11 q(s)