在使用PHP插入数据后重定向到页面时丢失会话中的用户ID

时间:2013-10-31 12:00:58

标签: php mysql

我需要一些关于这个问题的帮助,2个PHP文件,一个是另一个PHP代码将数据插入数据库的表单。我在会话中使用userID来保持为同一个用户输入的数据,我的问题是在我将数据提交到数据库后,我的页面必须重定向到同一会话中具有相同userID的另一个页面但显示输入的数据在表中,但每次我提交它都进入正确的页面,但没有userID,(437465375.php?userID =)如果我把用户ID放在最后(437465375.php?userID = 33)它去了显示数据的右页在表格中输入。

以下是表单的代码:

 <div class="posttext">
        <p><strong>PRIMARY INFORMATION TO BE FURNISHED BY A TAXPAYER AS REQUIRED BY SARS.</strong></p>
      <?php
       if (!isset($_SESSION['userID'])) {

        echo " <form method='post' id='contatti' action='savetrip1.php?userID='" . $_GET['userID']."'>";
      ?>
         <div id="contactform">

           <?php
           echo "<p><h2><strong>Kilometre Recording</strong></h2></p>";
           echo "<div class='commentfield'>";
           echo "<label for='author'>Work Km Travelled  ( Total Km only not Speedomiter meter reading): </label> <input type='text' value='".$row['travelledKm']."' name='travelledKm' id='TravelledKm' />";
           echo "</div>";
           echo "<div class='commentfield'>";
           echo "<label for='author'>Date: YYYY-MM-DD </label><input type='text' value='".$row['LoggedDate']."' name='LoggedDate' id='LoggedDate' />";
           echo "</div>";
           echo "<p><h2><strong>Business Travel Details</strong></h2></p>";
           echo "<div class='commentfield'>";
           echo "<label for='author'>From Where did you Travel: </label> <input type='text' value='".$row['fromk']."' name='fromk' id='fromk' />";
           echo "</div>";
           echo "<div class='commentfield'>";
           echo "<label for='author'>To Where did you Travel: </label> <input type='text' value='".$row['tok']."' name='tok' id='tok' />";
           echo "</div>";
           echo "<div class='commentfield'>";
           echo "<label for='author'>Reason: </label> <input type='text' value='".$row['Reason']."' name='Reason' id='Reason' />";
           echo "</div>";
           echo "<div class='commentfield'>";
           echo "<label for='author'> Fuel &amp; Oil Costs (R): </label> <input type='text' value='".$row['Fuel']."' name='Fuel' id='Fuel' />";
           echo "</div>";
           echo "<div class='commentfield'>";
           echo "<label for='author'>Repairs &amp; Maintenance Costs (R): </label> <input type='text' value='".$row['Repairs']."' name='Repairs' id='Repairs' />";
           echo "</div>";
           echo "<div class='contactbutton'>";
           echo "<input type='submit' class='contact-button' name='submit' id='invia'value='Save Trip' />"; 
           echo "<input type='reset' class='contact-button' name='clear' value='Clear Input' />";
           echo "</div>";
           echo "</form>";
           }
           ?>

      </div></div>
    </div>

这是用于将数据提交到mysql数据库的php代码:

    <?php
session_start('userID');


$host = "localhost";
$db = "database";
$user = "user";
$pass = "pass";

$userID=$_SESSION['ID'];
$travelledKm=$_POST['travelledKm'];
$LoggedDate=$_POST['LoggedDate'];
$fromk=$_POST['fromk'];
$tok=$_POST['tok'];
$Reason=$_POST['Reason'];
$Fuel=$_POST['Fuel'];
$Repairs=$_POST['Repairs'];

$conn = mysql_connect($host, $user, $pass);
mysql_select_db($db) or die(mysql_error());

if (isset($_POST['submit']) == true) {

  $query = "INSERT INTO   `webimckr_lockbook`.`trip_log`(`userID`,`LoggedDate`,`travelledKm`,`fromk`,`tok`,`Reason`,`Fuel`,`Repairs`)VALUES ('$userID','$LoggedDate','$travelledKm','$fromk','$tok','$Reason','$Fuel','$Repairs')";
  $result=mysql_query($query) or die(mysql_error());

if($result){

header("Location: 437465375.php?userID=".$_GET['userID']."");

}

else {
echo "ERROR";

} 

}

?>

<?php
// close connection
mysql_close();

?>  

我是PHP和Mysql的新手并且自己学习,请帮助我,以便我能够理解它并将来使用。

3 个答案:

答案 0 :(得分:0)

session_start()生成的phpsessionId对于每个浏览器都是唯一的,所以你只能查看你的表单帖子

每个刷新表单都会发布您的数据,无论您是否可以检查帖子是否为空

答案 1 :(得分:0)

这条线对我来说不合适:

$userID=$_SESSION['ID'];

你在顶部使用userID然后调用ID?使用相同的名称

答案 2 :(得分:-1)

使用$_POST['userID']代替$_GET['userID']