从列表 php 传递会话变量

问题描述 投票:0回答:1

我在通过选项列表中的链接传递会话变量时遇到一个主要问题。 搜索了很多帖子后,我没有看到任何有帮助的内容。 这是代码示例。

enter code here
session_start();
enter code here
foreach($clients 作为 $client){
enter code here
$_SESSION['name']=$client ->user_name;
enter code here
$_SESSION['title']=$client ->user_title;
enter code here
$_SESSION['client_id']=$client ->user_id;
enter code here
$_SESSION['info']=$client ->user_info;
enter code here
$_SESSION['type']=$client ->user_type;
enter code here
$_SESSION['project_ref']=$client ->user_project_ref;
enter code here
$_SESSION['client_project']=$client ->user_project;
enter code here
$client_id=$_SESSION['client_id']; }

然后在列表中我们有

enter code here

在“客户”页面中我们有

enter code here
session_start();

enter code here
if(isset($_SESSION['name'])) {

enter code here
$_SESSION['client_id'];
enter code here
$_SESSION['名称'] ;
enter code here
$_SESSION['标题'];
enter code here
$_SESSION['信息'];
enter code here
$_SESSION['类型'];
enter code here
$_SESSION['project'];//回显$project;
enter code here
$_SESSION['ref']; }

enter code here
get_results($wpdb->prepare(" SELECT * FROM vo_wp_clients where user_id= '$client_id'"));

页面打开,但结果不正确。 如果我回显会话变量,它会显示错误条目的设置。 我确信我错过了一些简单的东西,但任何帮助都会非常有帮助

html-select session-variables
1个回答
0
投票

听起来您在尝试从列表中选择客户端时遇到了会话变量的一些问题。

在我当前的 PHP 项目中,我面临类似的问题,所以我绝对可以帮助您解决这种情况!

这个东西可以帮助你获得输出。

  • 通过 URL 查询参数传递 client_id
  • 根据client_id检索目标页面上的客户详细信息。
  • 仅在获取正确的客户端数据后设置会话变量。
  • 确保清理输入以防止 SQL 注入。

1。显示客户列表

// When displaying clients
    foreach ($clients as $client) {
        echo '<a href="client_page.php?client_id=' . $client->user_id . '">' . $client->user_name . '</a>';
    }

2。在客户端页面设置会话变量

client_page.php

<?php

session_start();

if (isset($_GET['client_id'])) {
    $client_id = intval($_GET['client_id']); // Sanitize input
    // Query to get the client info
    $clients = $wpdb->get_results($wpdb->prepare("SELECT * FROM vo_wp_clients WHERE user_id = %d", $client_id));

    if (!empty($clients)) {
        $client = $clients[0]; // Get the first client record
        $_SESSION['name'] = $client->user_name;
        $_SESSION['title'] = $client->user_title;
        $_SESSION['client_id'] = $client->user_id;
        $_SESSION['info'] = $client->user_info;
        $_SESSION['type'] = $client->user_type;
        $_SESSION['project_ref'] = $client->user_project_ref;
        $_SESSION['client_project'] = $client->user_project;
    }
}

// In this way now you can use the session variables as needed
if (isset($_SESSION['name'])) {
    echo $_SESSION['name'];
    echo $_SESSION['title'];
    echo $_SESSION['info'];
    // and so on...
}


?>

© www.soinside.com 2019 - 2024. All rights reserved.