Have an account? Sign in
Login  Register  Facebook
PHP SQL Insert with Sessions
Hi I am trying to create a log system were users can log activities, once they are logged in their username is held in a session. My problem is I want them to be able to create a new log by filling out a form but I don\'t want them to have to enter their user name every time rather grab the username from the session and pop it into the insert statement. Here is the code I would greatly appreciate any help asap as it is for a project with looming deadline. Cheers.
<?php
session_start();
    if(!session_is_registered(\"myusername\")){
        header(\"location:main_login.php\");
    }
include \'topmembers.php\';
?>

<div id=\"page\" class=\"container\">
<!--First Post-->		<div id=\"content\">
			<div class=\"post\">

<?php

$con = mysql_connect(\"localhost\",\"root\",\"root\");
if (!$con)
  {
  die(\'Could not connect: \' . mysql_error());
  }

    mysql_select_db(\"newcastle\", $con);
    $sql=\"INSERT INTO log (logid, username, logdate, type, totaltime_mins, total_distance, notes)
     VALUES 
     (Null,
        \'\".$_SESSION[\'myusername\'].\"\',
            $_POST[logdate]\',
                \'$_POST[type]\',
                    \'$_POST[totaltime_mins]\',
                        \'$_POST[total_distance]\',
                            \'$_POST[notes]\')\";

echo $_SESSION[\'myusername\'];

?> 

<?php


if (!mysql_query($sql,$con))
  {
  die(\'Error: \' . mysql_error());
  }
echo \"Log Saved\";


mysql_close($con)
?> 

</div>
</div>
<?php
include \'websiteend.php\';
?>
Started: September 16, 2011 Latest Activity: September 16, 2011 php sql sessions
1 Answer
first session_is_registered has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged just use
if(isset($_SESSION[\'myusername\']))
just try this code and if not worked tell me the return of var_dump
<?php
session_start();
    include \'topmembers.php\';
    
    if(!isset($_SESSION[\'myusername\'])){
        header(\"location:main_login.php\");
    }
    //to check it 
    var_dump($_SESSION);

?>

<div id=\"page\" class=\"container\">
    <div id=\"content\">
			<div class=\"post\">

<?php

    $con = mysql_connect(\"localhost\",\"root\",\"root\");
    if (!$con){
        die(\'Could not connect: \' . mysql_error());
    }
    mysql_select_db(\"newcastle\", $con);

    $sql=\"INSERT INTO log (logid, username, logdate, type, totaltime_mins, total_distance, notes
    ) VALUES (
        Null,
        \'\".$_SESSION[\'myusername\'].\"\',
                $_POST[logdate]\',
                    \'$_POST[type]\',
                        \'$_POST[totaltime_mins]\',
                            \'$_POST[total_distance]\',
                                \'$_POST[notes]\')\";
    if (!mysql_query($sql,$con)){
        die(\'Error: \' . mysql_error());
    }else{
        echo \"Log Saved\";    
    }
    mysql_close($con)
?> 

</div>
</div>

Posted: MacOS
In: September 16, 2011

Doesn't seem to work, same problem as before :- Returns a blank page. As this only a school project and doesn't need to be very secure, is there an easier way of doing this?
September 16, 2011

and var_dump($_SESSION); return what?i think you did not set the $_SESSION
September 16, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer