-1

我需要一些关于这个问题的帮助,2 个 PHP 文件,一个是表单,另一个是用于将数据插入数据库的 PHP 代码。我在会话中使用用户 ID 来保留为同一用户输入的数据,我的问题是在我将数据提交到数据库后,我的页面必须重定向到具有相同用户 ID 的同一会话中的另一个页面,但显示输入的数据在表中,但每次我提交它都会转到正确的页面但没有用户ID,(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 非常陌生,并且自学,请帮助我,以便我理解它并为将来使用。

4

3 回答 3

0

这条线在我看来不正确:

$userID=$_SESSION['ID'];

您在顶部使用用户ID,然后调用ID?使用相同的名称

于 2013-10-31T12:13:01.890 回答
0

session_start()正在生成每个浏览器唯一的 phpsessionId,因此您只能查看您的表单帖子

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

于 2013-10-31T12:12:44.377 回答
-1

使用$_POST['userID']而不是$_GET['userID']

于 2013-10-31T12:08:20.460 回答