在用户的多个回发之间存储值

时间:2011-10-10 11:59:34

标签: php mysql

我的代码的想法是让用户选择航班,然后减少(-1)他选择的航班的航班座位。
当用户改变航班时,我减少新航班(-1)座位并增加以前的航班座位,因为用户改变了航班。

但我不知道如何保留以前的航班ID,实际上我尝试了但是我失败了,无论如何..

如何存储以前的航班ID?
因为每次通过单击提交按钮重新加载页面时都会重置变量(这就是发生的事情,因为每次打开页面时我都必须启动航班ID持有者变量。)

甚至可以举办类似的活动吗?......

这是我尝试使用会话的方法:

$flight_id = '';//empty
$_SESSION['prev_flight_id'] = $flight_id; // I just initiate the session now
echo $_SESSION['prev_flight_id']; // to make sure there's something here
// result is nothing(empty)  important note: I set the value of $flight_id 
// later in the script

我也试过这个:

  session_start();
  // bunch of code (I didn't create the $flight_id yet) then :
  // submiting the old flight id in the session (I GET THE ERROR HERE)
            $_SESSION['prev_flight_id'] = $flight_id;
            echo $_SESSION['prev_flight_id'];
            // get flight_id from text area
            $flight_id = $_POST['flight_id_res'];
              // result : Undefined variable: flight_id...

6 个答案:

答案 0 :(得分:1)

PHP

利用会话。

的MySQL

或者您可以使用MySQL SET @@global.your_var进行全局存储,SELECT来检索您的航班ID。

答案 1 :(得分:0)

亲爱的,您可以在使用该ID后在Session中存储以前的航班ID,然后取消设置会话中的值。简单

答案 2 :(得分:0)

你可以做一件事,在提交表单时在session_list(发送到服务器)中添加一个名为last_flight id的变量,就像Session一样,在服务器端,它会检查last_flight是否没有任何值如果是的话,它将增加座位数,并像以前那样完成其余的工作

答案 3 :(得分:0)

假设您在这种情况下使用用户表和flightplan表,那么您需要在user table和flightplan表之间建立关系表。即存储用户和flightplan号码的userflightplan表。通过使用此表,您可以使用更新触发器来实现飞行计划表中的座位增加/减少。通过访问OLD.flightplan增加和NEW.flightplan减少飞行计划表中相关飞行计划的席位。

答案 4 :(得分:0)

您需要跟踪user_id,并进行某种会话跟踪。

我将在MySQL中解决你的其余问题。

  

甚至可以举办类似的活动吗?......

当然,只要你有一个user_id,就可以从数据库中获取所有信息,这就是它的用途。

$user_id = mysql_real_escape_string($_SESSION['user_id']);
$sql = "SELECT b.flight_id, other fields... 
        FROM booking b 
        WHERE b.customer_id = '$user_id'
          AND b.not_yet_closed = 'TRUE'; 

如果您打开多个航班,则必须提供要更改的航班列表,或者您必须将当前的flight_id存储在$_SESSION变量中。

以下是更改预订航班号后如何更改座位数

为了跟踪航班上的可用座位,您需要有一张预订表和一张航班表。

booking
--------
id unsigned integer auto_increment primary key,
customer_id integer foreign key references customer(id),
flight_id foreign key references flight(id),
not_yet_closed BOOLEAN NOT NULL DEFAULT TRUE,
other fields....

flight
-------
id unsigned integer auto_increment primary key
available_seats
other fields ....

对于booking表格中的每次更改,您都会使用触发器更新航班中的可用座位:

DELIMITER $$

CREATE TRIGGER bi_booking_each BEFORE INSERT ON booking FOR EACH ROW
BEGIN
  DECLARE SeatsAvailable INTEGER;
  INSERT SUM(f.available_seats) INTO SeatsAvailable 
    FROM flight f 
    WHERE flight.id = NEW.flight_id;
  IF SeatsAvailable = 0 THEN 
    //Signal throws an error (in MySQL 5.5+)
    SIGNAL SQLSTATE 45000 
    SET MESSAGE_TEXT = CONCAT('Flight ',NEW.flight_id,' is full');
    //SELECT `Flight_is_full` FROM Error (use this in MySQL 5.1 and before to throw error) 
  END IF;
END $$

CREATE TRIGGER ai_booking_each AFTER INSERT ON booking FOR EACH ROW
BEGIN
  UPDATE flight 
    SET available_seats = available_seats - 1 
    WHERE flight.id = NEW.flight_id;
END $$


CREATE TRIGGER bu_booking_each BEFORE UPDATE ON booking FOR EACH ROW
BEGIN
  DECLARE SeatsAvailable INTEGER;
  INSERT SUM(f.available_seats) INTO SeatsAvailable 
    FROM flight f 
    WHERE flight.id = NEW.flight_id;
  IF SeatsAvailable = 0 THEN 
    //Signal throws an error (in MySQL 5.5+)
    SIGNAL SQLSTATE 45000 
    SET MESSAGE_TEXT = CONCAT('Flight ',NEW.flight_id,' is full');
    //SELECT `Flight_is_full` FROM Error (use this in MySQL 5.1 and before to throw error) 
  END IF;
END $$

CREATE TRIGGER au_booking_each AFTER UPDATE ON booking FOR EACH ROW
BEGIN
  UPDATE flight 
    SET available_seats = available_seats + 1 
    WHERE flight.id = OLD.flight_id;

  UPDATE flight 
    SET available_seats = available_seats - 1 
    WHERE flight.id = NEW.flight_id;
END $$

DELIMITER ;

答案 5 :(得分:0)

简单的逻辑可能是: 1.用户预订一个航班,以减少该航班的免费航班数量,此时您可以将此航班的ID保存到PHP会话中。 2.当用户改变主意并在知道上次预订航班的ID时预订另一航班您可以轻松地通过该ID获得免费座位,减少新航班的免费座位并存储新航班ID而不是最后一个航班ID。

简单的algorythm:

<?php
session_start();
$_SESSION['booked_flight_id'] = 0;

// ...

// user wants to book a flight
$_SESSION['booked_flight_id'] = book_flight($flight_id); // lets assume that function book_flight($flight_id) decreases free seats and returns the flight ID

// ...

// user changes his mind
unbook_flight($_SESSION['booked_flight_id']); // assuming that method unbook_flight($flight_id) will increase free seats
$_SESSION['booked_flight_id'] = book_flight($new_flight_id);

这有用吗?