views:

153

answers:

2

Hello all,

How is it possible that this function below inserts two rows in my database?

//called like
save_session_db($_SESSION['user_id']);

function save_session_db($session){
    include('includes/db-connect.php');
    $connectionInfo = array( 'Database' => 'Belior');
    $conn = sqlsrv_connect( $serverName, $connectionInfo);
    if($conn){
        $date = date('l jS \of F Y');
        $_SESSION['model_start_date'] = $date;
        $tsql = "INSERT INTO User_Session (user_id, date_created) VALUES ('$session', '$date')";
        echo 'insert--<br>'.$tsql;
        if(sqlsrv_query($conn, $tsql))  {
            return true;
        }else{
            return false;
        }
    }else{
           return false;
    }
    sqlsrv_close($conn);
}

This function is not called from anywhere else. When I comment the function call above nothing gets inserted into DB. And PHP does not complain about invalid function call, meaning it doesn't get called from anywhere else.

When I uncomment the function call two rows get inserted!!

Thanks all for any help. I am sure its something simple, I just can't figure it out. It was working before, I can't remember what I tweaked.

Update

It looks as if every single page that I have gets loaded twice?! Why? I don't have a htaccess file anywhere that does this?

127.0.0.1 - - [06/Jan/2010:18:34:00 +0000] "GET /webs/end/create.php HTTP/1.1" 200 44830
127.0.0.1 - - [06/Jan/2010:18:34:06 +0000] "GET /webs/end/create.php HTTP/1.1" 200 44830
+4  A: 

Is it possible that the code that call this function is called twice? For example, if your page is reload two times for some reason or another? Try to put some echo in the code that call this function. Maybe you'll get your answer.

David Brunelle
I have put in one echo that echos the var $tsql and that gets printed once.
Abs
If you check your access.log, how many times does a page get loaded if you refresh once? (which browser do you use?)
Jimmy Shelter
Good question. In my access log, it looks as if each page gets called twice - why is this the case? I don't have a htaccess file that does this anywhere. I updated question.
Abs
Which browser do you use? If you switch browsers does the page still load twice?
Jimmy Shelter
Deaar GOD! I found a dodgy htaccess file - with some rewrites. Epic Fail. Thanks David and Jimmy. :)
Abs
I guess it was being loaded too quick for me to notice and only one single thing will be outputted as I only see the second re-load.
Abs
+2  A: 

If it truly is only being executed once, then it would seem to indicate something going on at the server. The table User_Session could have a trigger that adds an additional row, for example. Although, that seems like something you would probably know about.

Mark Wilkins