单个提交按钮应在php中执行多项操作

时间:2019-01-23 09:54:43

标签: php html html5

我有两个php页面。

  1. dataentryform.php
  2. report.php

在dataentryform.php中,通过表单标签,我接受了19个用户输入,其中fcode是主键。提交后,我希望发生两件事。

  1. 数据存储到数据库中
  2. 在表格中输入的数据应显示在report.php中 文件。

我的问题:

  1. 能够将值存储到数据库中并检索它。
  2. 但是,在report.php文件上打印的值将始终 对应表中的第一行

我该如何解决?

dataentryform.php

<form method="post" action="includes/dbinsert.php">
        <table width="650" border="1" class="table1">
  <tbody>
    <tr>
      <td class="label">Farmer's Code</td>
      <td width="350" colspan="2"><input type="text" name="fcode" 
class="text" autocomplete="off" required></td>
    </tr>
    <tr>
      <td class="label">Farmer Name</td>
      <td width="350" colspan="2"><input type="text" name="fname" 
class="text" autocomplete="off" required></td>
    </tr>
    <tr>
      <td class="label">Village/ Town</td>
      <td colspan="2"><input type="text" name="village" class="text" 
autocomplete="off" required></td>
    </tr>
    <tr>
      <td class="label">Survey Number</td>
      <td width="350" colspan="2"><input type="text" name="surnum" 
class="text" autocomplete="off" required></td>
    </tr>
    <tr>
      <td class="label">Plot Number</td>
      <td width="350" colspan="2"><input type="text" name="plotnum" 
class="text" autocomplete="off" required></td>
    </tr>
    ...
    ...
    ...
    ...
    ...
    ...
    ...
    ...
    ...
    ...
    ...
    ...
    <tr>
      <td class="label">Potash (Tons/Acre)</td>
      <td width="350" colspan="2"><input type="number" name="potash" 
class="range" min="0" step="0.001" autocomplete="off" required></td>
    </tr>
  </tbody>
</table>
</div>
  <button type="submit" name="submit" class="submit">Submit</button>
</div>
</form>

dbinsert.php

<?php
include 'dbconnect.php';
$fcode = $_POST['fcode'];
$fname = $_POST['fname'];
$village = $_POST['village'];
$surnum = $_POST['surnum'];
$plotnum = $_POST['plotnum'];
$acre = $_POST['acre'];
$gunta = $_POST['gunta'];
$soiltype = $_POST['soiltype'];
$wtrsrc = $_POST['wtrsrc'];
$factory = $_POST['factory'];
$labnum = $_POST['labnum'];
$nextcrop = $_POST['nextcrop'];
$coldate = $_POST['coldate'];
$gendate = $_POST['gendate'];
$season = $_POST['season'];
$taryield = $_POST['taryield'];
$nitro = $_POST['nitro'];
$phos = $_POST['phos'];
$potash = $_POST['potash'];

$sql = "INSERT INTO forminfo (fcode,fname,village,surnum,plotnum,acre,gunta,soiltype,wtrsrc,factory,labnum,
                               nextcrop,coldate,gendate,season,taryield,nitro,phos,potash)
        VALUES ('$fcode','$fname','$village','$surnum','$plotnum','$acre','$gunta','$soiltype','$wtrsrc','$factory','$labnum',
                 '$nextcrop','$coldate','$gendate','$season','$taryield','$nitro','$phos','$potash');";
mysqli_query($conn, $sql);

header('location:../report.php');
?>

dbinsert.php正在将值插入数据库。然后重定向到report.php。在这里,我包括dbextract.php。但是,report.php中显示的值不正确。 尝试了这个也没有运气。

dbextract.php

 <?php
      include 'dbconnect.php';
      $fcode = $_POST['fcode'];
      $sql = "SELECT * FROM forminfo WHERE fcode=['$fcode']";
      $result = mysqli_query($conn, $sql);
      $row = mysqli_fetch_array($result); 
 ?>      
  

错误:无法识别的变量$fcode

2 个答案:

答案 0 :(得分:0)

您可以通过添加

使用会话变量
session_start();
$_SESSION['data'] = $_POST;

dbinsert.php的开头,然后在$fcode = $_SESSION['data']['fcode'];上使用dbextract.php

答案 1 :(得分:0)

要理解Twista的答案-最小,完整和可验证的示例可能类似于以下示例。

注意:

我几乎排除了引起任何类型错误的所有内容,我也省略了实际的数据库插入,因为它实际上与问题无关。

从提供的代码来看,实际上这是一个大问题:

未定义的索引。从提供的代码 dbinsert.php 来看,有很多。 $acre = $_POST['acre']及其后的每个$_POST[]都应产生Warning: Undefined index

dbextract.php 也有一个非常无效的SQL查询。它可能应该是SELECT * FROM forminfo WHERE fcode='$fcode'

实际上,如果您使用MySQLi Prepared Statements

下面的示例演示如何使用$_SESSION

dataentryform.php

<form method="post" action="dbinsert.php">
  <table width="650" border="1" class="table1">
    <tbody>
      <tr>
        <td class="label">Farmer's Code</td>
        <td width="350" colspan="2"><input type="text" name="fcode" class="text" autocomplete="off" required></td>
      </tr>
      <tr>
        <td class="label">Farmer Name</td>
        <td width="350" colspan="2"><input type="text" name="fname" class="text" autocomplete="off" required></td>
      </tr>
      <tr>
        <td class="label">Village/ Town</td>
        <td colspan="2"><input type="text" name="village" class="text" autocomplete="off" required></td>
      </tr>
      <tr>
        <td class="label">Survey Number</td>
        <td width="350" colspan="2"><input type="text" name="surnum" class="text" autocomplete="off" required></td>
      </tr>
      <tr>
        <td class="label">Plot Number</td>
        <td width="350" colspan="2"><input type="text" name="plotnum" class="text" autocomplete="off" required></td>
      </tr>
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      <tr>
        <td class="label">Potash (Tons/Acre)</td>
        <td width="350" colspan="2"><input type="number" name="potash" class="range" min="0" step="0.001" autocomplete="off" required></td>
      </tr>
    </tbody>
  </table>
  <button type="submit" name="submit" class="submit">Submit</button>
</form>

dbinsert.php

<?php
  session_start();
  // include 'dbconnect.php';
  $fcode = $_POST['fcode'];
  $fname = $_POST['fname'];
  $village = $_POST['village'];
  $surnum = $_POST['surnum'];
  $plotnum = $_POST['plotnum'];

  $_SESSION['fcode']=$fcode;

  // $sql = "INSERT INTO forminfo (fcode,fname,village,surnum,plotnum,acre,gunta,soiltype,wtrsrc,factory,labnum,
                                 // nextcrop,coldate,gendate,season,taryield,nitro,phos,potash)
          // VALUES ('$fcode','$fname','$village','$surnum','$plotnum','$acre','$gunta','$soiltype','$wtrsrc','$factory','$labnum',
                   // '$nextcrop','$coldate','$gendate','$season','$taryield','$nitro','$phos','$potash');";
  // mysqli_query($conn, $sql);

  header('location: dbextract.php');
?>

dbextract.php

<?php
  //session_start();
  // include 'dbconnect.php';
  $fcode = $_SESSION['fcode'];
  //$fcode = $_GET['fcode'];
  var_dump($fcode);
  // $sql = "SELECT * FROM forminfo WHERE fcode=['$fcode']";
  // $result = mysqli_query($conn, $sql);
  // $row = mysqli_fetch_array($result); 
?>

无需使用$_SESSION也可以完成相同的操作。 dataentryform.php 保持不变。

dbinsert.php

<?php
  //session_start();
  // include 'dbconnect.php';
  $fcode = $_POST['fcode'];
  $fname = $_POST['fname'];
  $village = $_POST['village'];
  $surnum = $_POST['surnum'];
  $plotnum = $_POST['plotnum'];

  //$_SESSION['fcode']=$fcode;

  // $sql = "INSERT INTO forminfo (fcode,fname,village,surnum,plotnum,acre,gunta,soiltype,wtrsrc,factory,labnum,
                                 // nextcrop,coldate,gendate,season,taryield,nitro,phos,potash)
          // VALUES ('$fcode','$fname','$village','$surnum','$plotnum','$acre','$gunta','$soiltype','$wtrsrc','$factory','$labnum',
                   // '$nextcrop','$coldate','$gendate','$season','$taryield','$nitro','$phos','$potash');";
  // mysqli_query($conn, $sql);

  header('location: dbextract.php/?fcode='.$fcode);
?>

dbextract.php

<?php
  //session_start();
  // include 'dbconnect.php';
  //$fcode = $_SESSION['fcode'];
  $fcode = $_GET['fcode'];
  var_dump($fcode);
  // $sql = "SELECT * FROM forminfo WHERE fcode=['$fcode']";
  // $result = mysqli_query($conn, $sql);
  // $row = mysqli_fetch_array($result); 
?>

以上显示,无需使用$_SESSION即可完成。例如,您将期望的值作为参数传递了。然后发生GET请求,然后您使用$_GET['fcode']来获取价值。

!!!重要!!!

上面的例子仅仅是例子。您可能需要对它们进行一些自定义以满足您的需求。例如,您将不得不取消对正在使用数据库的部分的注释,然后重新编写一下。

考虑通过单个POST请求向多个不同的.php文件发送数据-据我所知,在服务器端,有一个单个 POST请求将数据发送到单个文件。 注意 ,可以使用AJAX发送多个POST请求,但是AJAX在客户端运行,因此,如果在服务器端发生重定向,AJAX将无济于事。 / p>