我有两个php页面。
在dataentryform.php中,通过表单标签,我接受了19个用户输入,其中fcode
是主键。提交后,我希望发生两件事。
我的问题:
我该如何解决?
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
。
答案 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>