-1

I'm trying to put together a basic co-ordination app for my workplace, I have secured unique logins for each staff member. Part of the app is to have a display (using Ajax to keep up to date) showing who is available or unavailable for the next case load. Simply put; I can get the database to update using fixed values without much difficulty, but I want the update script to be dynamic in that it will update the correct users status.

This is the snippet from the update.php script;

<?php
    require_once("data/connect.php");
    $user_name = $_SESSION['user_name'];     
    mysqli_query($con,"UPDATE users SET status = 'AVAILABLE' WHERE user_name='$user_name'");    
    mysqli_close( $con );
    exit;
?>

As I said, above, if the value for the WHERE section is a fixed value, such as a particular users username - it works. However, once changed to the session variable, it does not. Any ideas?

UPDATE:

Ok so after following the below suggestions

<?php
    require_once("data/connect.php");
    session_start();
    $user_name = $_SESSION['user_name']; 
    var_dump($user_name);

    mysqli_query($con,"UPDATE users SET status = 'ONLINE' WHERE user_name = '$user_name'"); 

    mysqli_query( $con, $query );
    var_dump( $query, $con->error );
    mysqli_close( $con );
    exit;

I get this;

NULL NULL string(0) ""

Some more info; If I use the $_SESSION veriables in other ways, it works fine. Ie;

<?php
require_once("data/connect.php");

echo $_SESSION['user_name']; 
?>

This works fine to populate the various user names or what ever it is set to echo from the session, but when I try to use it as a variable for writing to the database/table, it then falls over. Even after placing 'session_start()' at the start.

Further update

I tried running the script in the main page, and on load it runs the script using the session variable! But still does not when running as a called file. Not sure what I've done wrong there.

2
  • 2
    have you checked before using session values .. session_start() should be in place
    – Sundar
    Sep 18, 2013 at 4:53
  • I've edited above showing the changes. I have added the session_start() - and have re-run the php script with the suggested debug lines in there as well. Got back; NULL NULL string(0) "" and it didn't perform the update.
    – Tactless
    Sep 18, 2013 at 5:33

3 Answers 3

0

Before any session information requests, you need to tell PHP you want to use session functionality: after the require line, but before the $user_name = $_SESSION['user_name']; line, enter session_start();

1
  • Wow thanks guys, wasn't expecting the help so quick! Anyway, I fixed the lack of the session_start() - and I got back this; NULL NULL string(0) ""
    – Tactless
    Sep 18, 2013 at 5:12
0

Session start is required before use the session array

<?php 

require_once("data/connect.php");

//you missed this
@session_start();

$user_name = $_SESSION['user_name']; 

//validate the user name
if(!empty($user_name))
{
    mysqli_query($con,"UPDATE users SET status='AVAILABLE'
    WHERE user_name='$user_name'"); 

    mysqli_close($con);
}else{

      echo "user name is empty";

     //check your session is proper or not
      print_r($_SESSION);


}
exit;
2
  • I fixed the lack of the session_start() - and I got back this; NULL NULL string(0) ""
    – Tactless
    Sep 18, 2013 at 5:13
  • This is interesting, it comes back with user name is emptyArray ( ) Yet other uses of the $_SESSION['user_name'] do populate with the data. I copied the above code exactly, and thats what comes back. Any ideas?
    – Tactless
    Sep 18, 2013 at 22:54
0

Issue appears to have been with my SESSION carry over to the php script that I have posted, being called by my primary page. I have encorporated it into the primary page and it executes upon log in & on log out instead.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.