将提交的值显示到下一个php页面

时间:2018-01-23 17:51:50

标签: php mysql

我正在尝试为我的预订网站制作验证页面,但我无法仅通过选择特定ID来显示特定数据。

例如,我提交了一个新客户,它生成了一个ID = 1.然后表单将把我带到另一个PHP页面,我希望它通过选择它的特定ID来显示我刚刚提交的客户的名字(是1或从之前生成的任何ID。)

这是我的首次提交表单

  <form action="menuactions/temporestoaction.php" method="post" enctype="multipart/form-data">   
   <label class="control-label">First Name:</label>
     <input class="form-control" placeholder="John" type="text" name="first_name" required autofocus/>
     <br />
   <label>Last Name:</label>
   <input class="form-control" placeholder="Doe" type="text" name="last_name" required/>
   <button type="submit" name="submit" class="btn btn-success btn-md">Submit</button>
  </form>

这是 temporestoaction.php ,它会将所有值提交到mysql数据库中:

<?php
 if(isset($_POST['submit']))   {
 $servername = "localhost";
 $username = "root";
 $password = "";
 $dbname = "records";

 //Form Inputs to Db
 $foodid = $_POST['foodid'];
 $firstname = $_POST['first_name'];
 $lastname = $_POST['last_name'];
 // Create connection
 $conn = mysqli_connect($servername, $username, $password, $dbname);
 // Check connection
 if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
 }

 $sql = "INSERT INTO `temporesto` ( first_name, last_name)
 VALUES ( '$firstname', '$lastname')";
 if (mysqli_query($conn, $sql)) {
 header('Location: ../temporesto.php?id='.$row['food_id'].'');
 exit();
 } else {
 echo "Error: " . $sql . "<br>" . mysqli_error($conn);
 }

 mysqli_close($conn);
 }
?>

然后将重定向到新的PHP页面 temporesto.php

<?php 
     include 'menuactions/temporestopick.php';
     while($row = mysqli_fetch_array($data, MYSQLI_ASSOC)){
         ?>

    <input type='hidden' value=" <?php echo $_GET['food_id'];?>" name="iduse">
    <label class="control-label">First Name: <h2><?php echo $row['first_name'];?></h2></label>
 <input class="form-control" type="text" name="first_name" />
 <br />
 <label class="control-label">Last Name: <h2><?php echo $row['last_name'];?></h2></label>
 <?php          
 }
 ?>

我遇到的问题是,它显示了提交的所有值,而不是特定值,see this image for reference.

P.S temporestopick.php 正在使用"SELECT * FROM temporesto";

2 个答案:

答案 0 :(得分:0)

如果您在处理表单之前尝试显示验证页面,那么我不会看到首先需要保存到数据库中。您只需在表单中发布所有表单值,它们就会包含在$_POST中,这是一个数组。然后,您清理所有输入,循环以获取其所有值,然后显示它们以进行验证。如果一切正常,则再次进行清理,然后插入数据库。

如果您有点迷路,可以执行以下操作。

  • 提交表单,并在表格中填写值。
  • 在您的操作页面中,清理表单中收到的所有值。
  • 您可以使用extract($_POST);将表单字段的值转换为字符串。
  • 显示查看和确认,然后在清理后将值提交到数据库。

样品: 的 process.php

<?php
if(isset($_POST['submit']))
{
   //sanitize your $_POST values
   foreach($_POST as $key => $value)
    {
        $_POST[$key] = sanitize($value);
        //You can out errors from empty fields here if you want
     }
     //extract $_POST values into strings
     extract($_POST);
     /*
     If your form has something like input name="fname" when you extract you will get the value for name as $name
      You can then echo Name: $fname
     */
      $values = <<<EOD
      Name: {$name}
      Email: {$email}
EOD;
}

我希望你能继续这样做。在此验证阶段之后,您可以保存到数据库,以避免将不需要的数据保存到数据库中。

答案 1 :(得分:0)

我删除了 temporestoaction.php 中的整个代码并添加了此内容:

<html>

<body>
    <?php
    if(isset($_POST['submit']))
    {
      //Form Inputs to Db
    $firstname = $_POST['first_name'];
    $lastname = $_POST['last_name'];
    $contact = $_POST['contact'];
    $eventdate = $_POST['eventdate'];
    $eventtime = $_POST['eventtime1'];
    $eventhours = $_POST['eventhours1'];
    $packages = $_POST['packages'];
    $food = $_POST['food'];
    $prices = $_POST['price-total'];
    $treats = $_POST['treats'];
    $chkfood = "";
    $chktreats = "";

    foreach($food as $chkfood1)  
       {  
          $chkfood.= $chkfood1.",";  
       }   
    foreach($treats as $chktreats1)  
       {  
          $chktreats.= $chktreats1.", ";  
       }   
    ?>
    <label><h2>NAME: <?php echo $firstname . " " . $lastname; ?></h2>        </label>
            <br/>
            <label><h2>Contact: <?php echo $contact; ?></h2></label>
            <br/>
            <label><h2>Food: <?php echo $chkfood; ?></h2></label>
            <br/>
            <label><h2>Event Date: <?php echo $eventdate; ?></h2></label>
            <br/>
            <label><h2>Event Time: <?php echo $eventtime; ?></h2></label>
            <br/>
            <label><h2>Event Hours: <?php echo $eventhours; ?></h2></label>
            <br/>
            <label><h2>Packages: <?php echo $packages; ?></h2></label>
            <br/>
            <label><h2>Food: <?php echo $chkfood; ?></h2></label>
            <br/>
            <label><h2>Prices: <?php echo $prices; ?></h2></label>
            <br/>
            <label><h2>Treats: <?php echo $chktreats; ?></h2></label>    
        </body> 
        <?php   
       }
        ?>
    </html>

使用此代码,它会从我的提交表单中获取所有输入,然后将其转移到下一页进行验证。