Ian Young
Ian Young

Reputation: 63

Passing session variable from list php

I am having a major issue passing session variables via link from a list of options. Having searched through numerous posts I havent seen anything that is of help. So here is a sample of the code.

enter code heresession_start(); enter code hereforeach($clients as $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']; }

Then in the list we have enter code here

in the 'client' page we have enter code heresession_start();

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

enter code here$_SESSION['client_id']; enter code here$_SESSION['name'] ; enter code here$_SESSION['title']; enter code here$_SESSION['info']; enter code here$_SESSION['type']; enter code here$_SESSION['project'];//echo $project; enter code here$_SESSION['ref']; }

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

The page opens but the result is incorrect. If I echo the session variables it shows the settings for the wrong entry. I'm sure I am missing something straightforward but any help would be very helpful

Upvotes: 1

Views: 23

Answers (1)

Meet Mendapara
Meet Mendapara

Reputation: 81

It sounds like you’re running into some issues with session variables when trying to select clients from a list.

In my current PHP project, I'm facing similar issues, so I can definitely help you to navigate this situation!

This Things can help you to get the Output.

  • Pass the client_id via a URL query parameter.
  • Retrieve client details on the target page based on the client_id.
  • Set session variables only after fetching the correct client data.
  • Make sure to sanitize inputs to prevent SQL injection.

1. Display the List of Clients

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

2. Set Session Variables on the Client Page

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...
}


?>

Upvotes: 1

Related Questions