Sav'vannis
Sav'vannis

Reputation: 3

$_SESSION not saving in database

Ok, I have a site that users have to log in to in order to submit a form, a character sheet. That form passes all the information to a page for inserting into a database. The very first thing on both the form page and the action page is

<?php
session_cache_expire(360);
session_start();
?>

Also on the form is a place where the $_SESSION['logname'] is echoed so you can see the user's name on the page:

<p>Logged in as:<br />
<?=$_SESSION['logname']?>

That is working, it correctly shows the person logged in. Now, on the page set in the action of the form, the code is:

$sql="INSERT INTO topdata (log_name,char_name,create_date) 
VALUES (\"$_SESSION[logname]\",\"$_POST[char_name]\",NOW())";

I have checked the fields in the database many times, and they are correct, however, the $_SESSION[logname] is not saving to the database, when looking at it via phpMyAdmin, the field is just blank. So the information for the $_SESSION[logname] is not making it from echoing on the form to being entered into the database. I have tried ['logname'] and [logname] and neither work.

I have also tried to add

<input type="hidden" name="log_name" value="<? $_SESSION['logname'] ?>" />
<input type="hidden" name="owner" value="<? $_SESSION['logname'] ?>" />

to the form, as well as

<td>Player Name:</td>
<td><? echo $_SESSION['logname'] ?></td>

and changed the INSERT to

$sql="INSERT INTO topdata (log_name,char_name,create_date) 
VALUES (\"$_POST[logname]\",\"$_POST[char_name]\",NOW())";

When viewing the form to fill it in, the Player Name is echoing the correct session information and it still isn't saving...

Any ideas on what I'm doing wrong and how I can get the session login to save in the player name field?

Any and all help would be greatly appreciated!

Upvotes: 0

Views: 183

Answers (3)

Arpita
Arpita

Reputation: 1398

Try using

$sql="INSERT INTO topdata (log_name,char_name,create_date) 
VALUES ('{$_SESSION['logname']}', '{$_POST['char_name']}' ,NOW())";

If it does not works then, write echo mysql_error(); after your mysql_query($sql) statement. echo mysql_error(); will print out actual error in your query. It will give you a clear idea about why the insert query is not executing properly.

Upvotes: 0

Deedar Ali Brohi
Deedar Ali Brohi

Reputation: 265

I think you are missing single quotation in query ' '
your code is

$sql="INSERT INTO topdata (log_name,char_name,create_date) 
VALUES (\"$_SESSION[logname]\",\"$_POST[char_name]\",NOW())";

i think you should use single quotation in $_SESSION['logname'] and $_POST['char_name']

$sql="INSERT INTO topdata (log_name,char_name,create_date) 
VALUES (\"$_SESSION['logname']\",\"$_POST['char_name']\",NOW())";

Upvotes: 2

deviloper
deviloper

Reputation: 7240

Well I think you are missing some single quotations there with your variables:

$sql="INSERT INTO topdata (log_name,char_name,create_date) 
VALUES (\"$_POST['logname']\",\"$_POST['char_name']\",NOW())";

or you could simply use local variables:

$logname = $_SESSION['logname'];
$char_name = $_SESSION['char_name'];
$sql="INSERT INTO topdata (log_name,char_name,create_date) 
VALUES (".$logname.", ".$char_name." ,NOW())";

Upvotes: 1

Related Questions