Jahed Hussain
Jahed Hussain

Reputation: 113

Why Isnt The Session Being Inserted Into Database?

Why can't I insert my session into my database? I'm sure I have used the same line of code elsewhere numerous times and it has worked for me till now.

<?php
$id = $_GET['election'];
$vote = mysql_real_escape_string($_REQUEST['vote']);
$vote = mysql_real_escape_string($_REQUEST['vote']);

$sql= "INSERT INTO votes (election_id, ni) VALUES ('$id', '". $_SESSION['ni']."')";  

if (!mysql_query($sql))
  {
        die('Error: ' . mysql_error());
  }
  else 
  {

      echo '<hr><h4>Your Vote Has Now Been Casted </h4><hr><h5> <a href = "votenow.php">My Account</a></h5>';

  }
?>

any ideas? the field ni comes out with the number 0

Upvotes: 0

Views: 86

Answers (2)

James Brooks
James Brooks

Reputation: 658

I don't think this has anything to do with being a database specific issue, if before that code runs if you insert:

var_dump($_SESSION['ni']);
exit;

Does $_SESSION['ni'] contain anything other than 0? Also have you called session_start() before you try and access the $_SESSION object?

http://php.net/manual/en/function.session-start.php

Upvotes: 1

Odinn
Odinn

Reputation: 808

Try using

session_start();

at the beginning of the file so the session variable will be set and initialized.

Upvotes: 1

Related Questions