如何将重试的数据库数据传递到另一个页面?

时间:2016-03-26 13:36:17

标签: php mysql forms mysqli

我正在尝试制作一份简单的预订表格。 1st显示数据库页面中的可用城市和包 当用户点击页面中的BOOK按钮时,页面将重定向到操作页面,其中所选表单/包应自动添加到booking表格并发送确认邮件。

以下代码从数据库生成可用的包:

include 'inc/db_connect.php';

//与DB连接工作

$sql="SELECT pkg, fcity, dcity, price, details FROM tour_package WHERE fcity='".$fcity_search."'  ORDER BY fcity ASC"; 
$sqlresult=mysqli_query($con,$sql);
$afct=mysqli_affected_rows($con);
while($pkg=mysqli_fetch_array($sqlresult,MYSQLI_ASSOC)){ 
    $num[]=$pkg['pkg'];
    $fcity[]=$pkg['fcity'];
    $dcity[]=$pkg['dcity'];
    $price[]=$pkg['price'];
    $details[]=$pkg['details'];
    $num++;
}

$i=0;
$book_btn_id = 1;
while($i < $afct)
{
    echo '<form action="book_tour_action.php">';
            $num_var_to_db = $num[$i];
        echo '<span class="search-class-row"><div class="search-results-label">Package : </div>'.$num[$i].'<br/></span>';
        echo '<span class="search-class-row"><div class="search-results-label">From : </div>'. $fcity[$i].'<br/></span>';
        echo '<span class="search-class-row"><div class="search-results-label">Destination : </div>' . $dcity[$i].'<br/></span>';
        echo '<span class="search-class-row"><div class="search-results-label">Price : </div>' .$price[$i].'<br/></span>';
        echo '<span class="search-class-row"><div class="search-results-label">Details :</div>' . $details[$i].'<br/></span>';
        //echo '<a href="#" class="booking-btn" name="bb'.$book_btn_id++.'"> BOOK </a>';
        echo '<input type="submit" value="BOOK" class="booking-btn" name="bb'.$book_btn_id++.'">';
        echo '<br><br/><hr/>';                          
        $i++;
    echo '</form>';
}

我已为BOOK按钮设置动态名称。

book_tour_action.php:

echo $num_var_to_db;
$sql = "INSERT INTO booking (num_var_to_db)
VALUES ('$num_var_to_db')";

但是当点击BOOK按钮时它显示UNDEFINED VARIABLE。

这意味着数据不会传递给book_tour_action.php我猜。 我怎样才能使它发挥作用?

1 个答案:

答案 0 :(得分:1)

您需要在表单中的隐藏输入字段中保留$num_var_to_db值才能获取值。尝试如下:

  echo '<form action="book_tour_action.php">';
  $num_var_to_db = $num[$i];
  echo '<input type="hidden" name="num_var_to_db" value="'.$num_var_to_db.'">'
       echo '<span class="search-class-row"><div class="search-results-label">Package : </div>'.$num[$i].'<br/></span>';
      echo '<span class="search-class-row"><div class="search-results-label">From : </div>'. $fcity[$i].'<br/></span>';
   echo '<span class="search-class-row"><div class="search-results-label">Destination : </div>' . $dcity[$i].'<br/></span>';
      echo '<span class="search-class-row"><div class="search-results-label">Price : </div>' .$price[$i].'<br/></span>';
      echo '<span class="search-class-row"><div class="search-results-label">Details :</div>' . $details[$i].'<br/></span>';
     //echo '<a href="#" class="booking-btn" name="bb'.$book_btn_id++.'"> BOOK </a>';
     echo '<input type="submit" value="BOOK" class="booking-btn" name="bb'.$book_btn_id++.'">';
      echo '<br><br/><hr/>';                          
       $i++;
 echo '</form>';

并在php页面中,您可以执行以下操作来获取值:

$num_var_to_db = $_REQUEST['num_var_to_db'];