开发者

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 da开发者_JAVA技巧ta 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.


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.


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.


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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜