dotwc62080 2016-07-24 08:59
浏览 38

将会话变量传递给另一个页面

I am trying to pass session variable to the next page, index.php but i have gotten an error undefined index on foreach($_SESSION['result'] as $row). May i know what's wrong?

<?php
session_start();
$search = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
$search = $db->real_escape_string($search);

if (strlen($search) >= 1 && $search !== ' ') {
    $query = 'SELECT * FROM tablename WHERE name LIKE "%'.$search.'%"';
    $result = $db->query($query) or trigger_error($db->error."[$query]");


    while($results = $result->fetch_array()) {
        $resultArray[] = $results;
    }   

    if (isset($resultArray)) {
        foreach ($resultArray as $result) {
            $show_name = preg_replace("/".$search."/i", "<b class='highlight'>".$search."</b>", $result['name']);
            $show_url = 'index.php';
            $out = str_replace('name', $show_name, $html);
            $out = str_replace('url', $show_url, $out); 
            $_SESSION['result']= $result['name'];
            echo($out);
        }
    }
}
?>

index.php

session_start();
<table>
<?php foreach($_SESSION['result'] as $row){ ?>
            <tr>
              <td>
                <?php echo $row['name'];?>
              </td>
              <td>
                <?php echo $row['description'];?>
              </td>
            </tr>
<?php } ?>
</table>
  • 写回答

2条回答 默认 最新

  • doufei8250 2016-07-24 09:04
    关注

    You never start the session in the first file. Start the file with

    <?php    
    session_start();
    

    Because you're missing that, when you assign a value to $_SESSION['result'] after your queries, nothing is saved in session.

    There is another error. You mean for $_SESSION['result'] to be an array, but you're saving it as a string. Change:

    $_SESSION['result']= $result['name'];
    

    To

    $_SESSION['result'][]= $result['name'];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)