繁体   English   中英

将会话数据插入mysql数据库

[英]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();

?>

我想将会话变量插入数据库。

这是我的代码,没有错误发生,页面被重定向到booking_detail.php,但是这些会话变量也没有插入到我的数据库中。

Fromto保留字,请使用反引号

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']}')";

注释掉header() ,使用error_reporting(-1)打开错误报告,检查mysql_error() ,然后解决该问题。

从现在开始,我可以看到您在sql查询中遇到语法错误,因为您使用from作为列名,它是受限词。 您必须将其放在`中。

1)如果会话是单独的脚本,则开始会话。
2)按照查询中@Mihai的建议删除保留的关键字。
3)在您的查询中,应为VALUES(而不是VALUES
4)正如您在评论中提到的那样, leaving_from不插入Db。 因为您没有在脚本中为$_SESSION['source_point']分配会话值。

在您的脚本中将是:-

<?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,

首先从所有会话变量中删除引号,例如:

{$_SESSION['source_point']}

其次,您要在mysql_error检查之前进行重定向,首先检查结果和错误,然后进行重定向:

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

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

从顶部删除空间

<?php session_start(); 

如果这不起作用

var_dump($_SESSION)插入之前要检查会话中是否存在值并使用die(mysql_error()); 与查询

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

}

如果chksbmt是提交按钮的名称,则在提交表单后将执行上述代码。 插入之前,它会转到标题中提到的页面。 在大括号之间写下所有内容,使用

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

  }
}

我希望我已经了解了您的问题,这可以锻炼身体。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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