【问题标题】:Storing $_POST with multi-dimensional array in mysql在mysql中使用多维数组存储$_POST
【发布时间】:2016-05-12 04:01:46
【问题描述】:

我有一个日历,每天都有一个这样的复选框:

    <input type="hidden" name="uevent['.$urange.']['.$list_day.']['.$month.']['.$year.']" value="0"><input type="checkbox" id="calendarcheck" name="uevent['.$urange.']['.$list_day.']['.$month.']['.$year.']" checked>$list_day

其中 $urange 是三种日历中的一种。

当点击提交时,我想每天存储在mysql中,不管它是否被选中。所以我生成了这段代码:

    print_r($_POST);

    $big_query='INSERT INTO `sdcheckings` (`email`,`urange`,`cyear`,`cmonth`,`cday`,`dchecked`,`tcheck`) VALUES ';

    for($i=0;$i<count($_POST[$row[0]]);$i++)
    {
        for($j=0;$j<count($_POST[$row[0]][$i]);$j++)
        {
            for($k=0;$k<count($_POST[$row[0]][$i][$j]);$k++)
            {
                if($k!=0)
                    $big_query.=', ';
                $dchecked = $_POST[$row[0]][$i][$j][$k];
                $big_query.='('.$_SESSION['email'].',  '.$row[0].',  '.$i.',  '.$j.',  '.$k.', '.$dchecked.', CURRENT_TIMESTAMP) ';
            }
        }
    }
     $adddates = mysql_query($big_query);

当我通过检查几个日期(2016 年 2 月 4 日和 2016 年 2 月 12 日)对其进行测试时,我得到了以下结果:

数组 ( [uevent] => 数组 ( [1] => 数组 ( [4] => 数组 ( [02] => 数组 ( [2016] => on ) [3] => 数组 ( [2016] => 0 ) ) [5] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [6] => 数组([02] => 数组([2016] => 0)[3] => 数组([2016] => 0)) [7] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [8] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [9] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [10] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [11] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [12] => 数组 ( [02] => 数组 ( [2016] => on ) [3] => 数组 ( [2016] => 0 ) ) [13] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [14] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [15] => 数组([02] => 数组([2016] => 0)[3] => 数组([2016] => 0)) [16] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [17] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [18] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [19] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [20] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [21] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [22] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [23] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [24] => 数组([02] => 数组([2016] => 0)[3] => 数组([2016] => 0)) [25] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [26] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [27] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [28] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [29] => 数组 ( [02] => 数组 ( [2016] => 0 ) [3] => 数组 ( [2016] => 0 ) ) [1] => 数组 ( [3] => 数组 ( [2016] => 0 ) ) [2] => 数组 ( [3 ] => 数组 ( [2016] => 0 ) ) [3] => 数组 ( [3] => 数组 ( [2016] => 0 ) ) [30] => 数组 ( [3] => 数组 ( [2016] => 0 ) ) [31] => 数组 ([3] => 数组 ([2016] => 0 ) ) ) ) [提交] => 更新)

数据库没有存储任何东西。我怀疑我的循环不正确。怎么可能?

在最后打印出 $big_query 时,我得到了这个:

INSERT INTO `sdcheckings` (`email`,`urange`,`gender`,`cyear`,`cmonth`,`cday`,`dchecked`,`tcheck`) VALUES

【问题讨论】:

  • 我希望你知道 SQL 本质上是二维的。
  • php 不会发布未选中的复选框
  • 发布未选中复选框的解决方案也是放置一个与可能未选中的复选框同名的隐藏输入。这样,如果未选中复选框,隐藏输入仍然成功并发送到服务器,但如果选中复选框,它将覆盖之前的隐藏输入。
  • @LuthandoLoot 或在 php 中,您可以使用三元,而不是隐藏输入 - 即。 $value = isset($_POST['checkbox']) ? true : false;。此外,隐藏的输入需要在表单文档中的复选框之前,否则即使选中它也会覆盖复选框。
  • @Sean 是的,你是对的。我认为我们已经为 OP 提供了足够的信息来解决他/她的问题(未选中的框)

标签: javascript php mysql multidimensional-array calendar


【解决方案1】:
        foreach($_POST['uevent'][$row[0]] as $yearnum => $postyear)
        {
            foreach($postyear as $monthnum => $postmonth)
            {
                foreach($postmonth as $daynum => $postday)
                {
                    if($firstcoma>0)
                        $big_query.=', ';
                    $firstcoma++;

                    if(strcmp($postday,"on")==0)
                        $postday=1;

                    $big_query.='("'.$_SESSION['email'].'",  '.$row[0].',  "'.$row[1].'",  '.$yearnum.',  '.$monthnum.',  '.$daynum.', '.$postday.', CURRENT_TIMESTAMP) ';
                }
            }
        }

【讨论】:

    猜你喜欢
    • 2011-10-23
    • 2013-05-07
    • 2017-06-01
    • 2010-10-27
    • 2017-11-12
    • 2011-09-12
    • 2017-11-12
    • 2011-07-26
    • 1970-01-01
    相关资源
    最近更新 更多