Novice_2011
Novice_2011

Reputation: 29

Form Based Arrays

I'm trying to access each value in a array without serializing it. I than want this data stored into a datetime field in MySQL. The data is received from my form [collect] => Array ( [0] => 01 [1] => 06 [2] => 2011 [3] => 17 [4] => 41 )

How can i store this array data into my database 'datetime' field column?

$dd=$_POST('$collect[0]'); 
$mm=$_POST('$collect[1]'); 
$yy=$_POST('$collect[2]'); 
$hh=$_POST('$collect[3]'); 
$ii=$_POST('$collect[4]');
$ddate = ('$yy','$mm','$dd',NULL,'$hh','$ii',NULL)

What i'm trying to do above is take each array value and store it in variable names and than use variable $ddate to store as a datetime in my database. I'm not sure if this is even possible, but i have tried serialize, but date will not insert into database just shows format 0000/00/00 00:00 on database so i'm trying the above to see if this can work.

If there's a universal way I can use instead of the above, please can someone show me a small example which I can work from.

Upvotes: 0

Views: 56

Answers (3)

mkilmanas
mkilmanas

Reputation: 3485

From your example I don't really understand in what language you are coding, but in PHP it would be like this:

$time = mktime(
    $_POST['collect'][3],
    $_POST['collect'][4],
    0,
    $_POST['collect'][1],
    $_POST['collect'][0],
    $_POST['collect'][2],
);

$datetime = date('Y-m-d H:i:s', $time);

Please refer to mktime() documentation for the order of arguments

Also, refer to date() if you need another formatting of the date.

Upvotes: 1

AJ.
AJ.

Reputation: 28184

Try this to get the data out of your POST request:

$collect = $_POST['collect'];
$dd=$collect[0];
$mm=$collect[1];
$yy=$collect[2];
$hh=$collect[3];
$ii=$collect[4];

Upvotes: 0

Michael J.V.
Michael J.V.

Reputation: 5609

From what you wrote, I'm assuming that the form field is called collect[] seeing you're trying to access its indexes.

However, you're using wrong method here - $_POST is an array, yet you tried to use it as a function (not that it's impossible, but for this purpose - it's really not the way to do it).

$dd = $_POST['collect'][0];
$mm = $_POST['collect'][1];

And so on until you reach the last element of your array.

If you are storing some user-submitted date to the MySQL - yes, there are better ways to do it. If you wish to know what they are, feel free to comment so I'll post my views on what better ways exist.

Upvotes: 0

Related Questions