简体   繁体   中英

Inserting session data to mysql database

<?php
session_start(); 
if (!isset($_SESSION)){
}
$total_amt=$_POST['total_amt'];
   $total_seats=$_POST['total_seats'];
   $boarding_point=$_POST['boarding_point'];
   $_SESSION['total_amt']=$total_amt;
   $_SESSION['total_seats']=$total_seats;
   $_SESSION['boarding_point']=$boarding_point;
?>
<?php
require_once("config.php");
$source_point=$_SESSION['source_point'];
$destination=$_SESSION['destination'];
$datepick=$_SESSION['datepick'];
$_SESSION['total_amt']=$total_amt;
$_SESSION['total_seats']=$total_seats;
$boarding_point=$_POST['boarding_point'];

// Insert data into mysql
$sql="INSERT INTO book_seat(from, to, datepick, total_amt, total_seats,    boarding_point) VALUES 
    '{$_SESSION['source_point']}',
    '{$_SESSION['destination']}',
            '{$_SESSION['datepick']}',
    '{$_SESSION['total_amt']}',
            '{$_SESSION['total_seats']}',
        '{$_SESSION['boarding_point']}')";
$result=mysql_query($sql);
if(isset($_POST['chksbmt']) && !$errors) 
{
    header("location:booking_detail.php");
}
if(!$sql) die(mysql_error());

mysql_close();

?>

I want to insert my session variables to my database..

This is my code, there is no error happening, page is redirecting to booking_detail.php but also these session variables are not getting inserted to my database also..

From and to are reserved word,use backticks

Reserved words in Mysql

$sql="INSERT INTO book_seat(`from`, `to`, datepick, total_amt, total_seats,    boarding_point) VALUES 
    '{$_SESSION['source_point']}',
    '{$_SESSION['destination']}',
            '{$_SESSION['datepick']}',
    '{$_SESSION['total_amt']}',
            '{$_SESSION['total_seats']}',
        '{$_SESSION['boarding_point']}')";

Comment out your header() , turn on error reporting using error_reporting(-1) , check mysql_error() and then fix that problem.

From now I can see that you've got syntax error in sql query because you're using from as column name which is restricted word. You have to put it in `.

1) Start session if its separate script.
2) Remove reserved keyword as suggested by @Mihai in your query.
3) In your query It should be VALUES( instead of VALUES .
4) As you are mention in your comment leaving_from not inserting into Db. Because in your script you have not assign session value for $_SESSION['source_point'] .

In your script will be :-

<?php
session_start(); 
   if (!isset($_SESSION)){
}

$total_amt  =   $_POST['total_amt'];
$total_seats =  $_POST['total_seats'];
$boarding_point =   $_POST['boarding_point'];

$_SESSION['total_amt']  =   $total_amt;
$_SESSION['total_seats']    =   $total_seats;
$_SESSION['boarding_point'] =   $boarding_point;
// Set here session value for $_SESSION['source_point'] as well,

First remove quotes from all session variables like:

{$_SESSION['source_point']}

Second you're redirecting before mysql_error check, Check on results and error first and then redirect:

if (!$result) {
  die(mysql_error());
}

if(isset($_POST['chksbmt']) && !$errors) 
{
 header("location:booking_detail.php");
}

remove the space from top

<?php session_start(); 

if this didn't work

var_dump($_SESSION) before inserting to check value exist in the session and use die(mysql_error()); with the query

$result=mysql_query($sql) or die(mysql_error());;
if(isset($_POST['chksbmt']) && !$errors) 
{
header("location:booking_detail.php");

}

Above code will be executed once the form is submitted if chksbmt is the name of the submit button. It takes to that page mentioned in header before inserting. Write all your stuff in between above curly braces, use

if(isset($_POST['chksbmt']) && !$errors) 
{
 //all your stuff, ex:storing in DB.
  if($result){
   header("location:booking_detail.php");

  }
}

I hope that I've understood your problem, this will workout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM