1

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

1 Answer 1

1

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


?>

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much that appears to have sorted the problem That has been bursting my brain for the last week!
Maybe this is a daft question, but shouldnt client_id=2103702968 be hidden?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.