dongmei8209 2014-07-27 15:31
浏览 41

使用已发布变量删除时无法取消设置$ SESSION变量

This page basically displays a table of a list of options from DB that a user can select, once selected the user is give the option to remove the selected option should they have made a mistake. I have taken out all of the unnecessary stuff to make it easier to follow. I have tried every combination to get a session array element to unset and have pretty much given up. I know the posted values are working ok as i have had them echoing back to the screen after submitting the form. The array_search function does not seem to like a variable name, if I replace $ss_id with a physical value I can get it to unset ok.

Any suggestions would be greatly appreciated.

<?php require_once('Connections/fs1.php'); 

    if (!isset($_SESSION)) {
      session_start();
    }

    if (!isset($_SESSION['con29']))
    {
    $_SESSION['con29'] = array();
    }

    //db call to get con29 options
    $query1 = mysql_query("SELECT * FROM sub_search_content") or die(mysql_error());

    $_SESSION['main_search_selected'] = 's7';
    $main_search_selected = $_SESSION['main_search_selected'];

    //setup the session variables depending on what was posted------------------

    if (isset($_POST['sub_search_content_id']))//add an option

        {
            $row_id_1 = $_POST['sub_search_content_id']; 
            array_push($_SESSION['con29'],$row_id_1);
        }

    if (isset($_POST['sub_search_content_id_remove']))
    {
        $ss_id = $_POST['sub_search_content_id_remove'];
        $key=array_search($ss_id,$_SESSION['con29']);
        if($key!==false) unset($_SESSION['con29'][$key]);
        //echo $ss_id;
        //echo $key;
    } 

    //print_r ($_SESSION['con29']);

    ?>
    <table width="100%" border="0" cellpadding="0" cellspacing="0">
    <?php


        while ($row = mysql_fetch_assoc($query1)) 
            {


                    $sub_search_id = $_SESSION['con29'];

                    if($row[$main_search_selected] == 1)
                     {
                     $bgcolour = '#ffffff';
                     $select_row ='';
                     $select_row_2='';
                     }

                    elseif($row[$main_search_selected] == 2)
                     {

                         $id = $row['sub_search_content_id'];
                         $key = array_search($id, $sub_search_id);

                         if($key != '')
                                {
                                    $bgcolour = '#AFC137';
                                    $select_row = '<input name="submit" class="push_buttons" type="submit" value="Remove" />';
                                    $select_row_2 = '<input name="sub_search_content_id_remove" type="hidden" id="sub_search_content_id_remove" value="' . $row['sub_search_content_id'] .' " />';  
                                }
                         else
                                {
                                    $bgcolour = '#ffffff';
                                                                    $select_row = '<input name="submit" class="push_buttons" type="submit" value="select" />';
                                    $select_row_2 = '<input name="sub_search_content_id" type="hidden" id="sub_search_content_id" value="' . $row['sub_search_content_id'].'" />';  
                                }
                     }

                     elseif($row[$main_search_selected] == 3)
                     {

                                    $bgcolour = '#FF9900';
                                    $select_row = "Included";
                     }

    //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------


                            if($row[$main_search_selected] != 1)
                            { ?>

                                              <tr bgcolor="<?php echo $bgcolour;?>" >
                                                <form action="test1.php" method="POST" name="form1" id="form1"   >
                                                  <?php echo $select_row_2; ?>
                                                  <td width="60" height="25" align="center" valign="middle" class="top-edge-border-menu"><?php echo $row['sub_search_content_id']; ?></td>
                                                  <td align="center" valign="middle" class="top-and-right-edge-border-left"><?php echo $row['sub_title']; ?></td>
                                                  <td height="25" align="center" valign="middle" class="top-and-right-edge-border">price</td>
                                                  <td height="25" align="center" valign="middle" class="top-edge-border"><?php echo $select_row; ?></td>
                                                </form>
                                              </tr>
                                              <?php }
                     } ?>

                                            </table></td>
                                          </tr>
                                      </tbody>
                                    </table>
    <?php mysql_free_result($query1); ?>
  • 写回答

1条回答 默认 最新

  • dpi9530 2014-07-27 15:51
    关注

    value="' . $row['sub_search_content_id'] .' "

    Notice the trailing space there? When you're posting the ID back to the script to remove it from the session, you're appending a space to the ID - this prevents array_search from finding it in your session.

    Just change that line to:

    $select_row_2 = '<input name="sub_search_content_id_remove" type="hidden" id="sub_search_content_id_remove" value="' . $row['sub_search_content_id'] . '" />';
    

    Note - if you need to check a variable in detail, var_dump() is far superior to an echo() - it'll show you things like spaces and variable types.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看