PHP 重定向导致包含失败
PHP redirect causes include to fail
我有 4 个 php 文件,其中 2 个是表单。在第一个表单中,用户输入一个数字并提交第二个 php 文件(表单操作)检查该数字是否存在于 sql 数据库中。如果是,它会将用户重定向到第三个 php 文件,该文件是其余详细信息的表单。现在,在将所有用户输入添加到数据库的最终 php 文件中,我有一个包含 secondfile.php 来调用用户输入的号码,以便可以将其添加到不同 [=37= 的数据库中] 比它被检查的那个。它基本上是 link 两个 table 在一起的方法。我已经尝试了几个测试,但除了这种情况外,我唯一的理论是它失败是因为重定向 header(/var/etc/whatever/thirdfile.php)。任何人都可以告诉我解决它的方法或替代方法吗?
文件 1:
<html>
<body>
<p>Enter the reference number of you request<i>(this should have been sent to you by the sponsor)</i>:</p>
<form action="2.php" method="POST">
<fieldset align="left">
<table>
<tr>
<td>Reference Number:</td><td><input type="text" name="REFERENCE_NO" size="10" /></td>
</tr>
<input type="submit" value="Submit">
</table>
</body>
</html>
文件 2:
<html>
<?php
$check = $_POST["REFERENCE_NO"];
$servername = "x.x.x.x";
$username = "SarahSmith";
$password = "something123";
$dbname = "DB";
#connect to MySQL server
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error){
die ("Connection failed: " . $conn->connect_error);
}
#check reference number exists
$sql ="SELECT REF FROM TEST WHERE REF='$check';";
#check if query returned a row
$result = $conn->query($sql);
if ($result->num_rows == 1){
header ("Location: http://www.example.com/3.php");
}
else {echo "ERROR";}
$conn->close();
?>
</html>
文件 3:
<html>
<form action="4.php" method="POST">
<fieldset align="left">
<legend>Requestor Form</legend>
<p><i>To be completed by the requestor</i></p>
<table>
<tr>
<td>Name:</td><td><input type="text" name="REQUESTOR_NAME" size="40" /> </td>
</tr>
<tr>
<td>Department/Position:</td><td><input type="text" name="DEPARTMENT_POSITION" size="40" /></td>
</tr>
<tr>
<td>Telephone Number:</td><td><input type="text" name="REQUESTOR_TELEPHONE_NUMBER" size="40" maxlength="11" /></td>
</tr>
<tr>
<td>Email Address:</td><td><input type="email" name="REQUESTOR_EMAIL" size="40" /></td>
</tr>
<tr>
<td>Project Manager Name:</td><td><input type="text" name="PROJECT_MANAGER_NAME" size="40" /></td>
</tr>
<tr>
<td>Project Mandate Number:</td><td><input type="text" name="PROJECT_MANDATE_NUMBER" size="40" /></td>
</tr>
<tr>
<td>Project Title:</td><td><input type="text" name="PROJECT_TITLE" size="40" /></td>
</tr>
<tr>
<td>What are the Project requirements:</td><td><textarea placeholder="Please detail exact requirements or contact us on the above e-mail address in order to discuss your requirements prior to submitting your request" cols="42" rows="10" name="REQUIREMENTS" id="REQUIREMENTS" ></textarea></td>
</tr>
<tr>
<td>What are the Project timescales:</td><td><input type="date" name="TIMESCALE" size="40" /></td>
</tr>
<input type="submit" value="Sumbit">
</form>
文件 4:
<?php
$servername = "x.x.x.x";
$username = "SarahSmith";
$password = "something123";
$dbname = "DB";
include_once '2.php';
#connect to MySQL server
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error){
die ("Connection failed: " . $conn->connect_error);
}
#create MySQL query to add new record to TEST table
$sql = "INSERT INTO REQ (REFERENCE_NO, REQUESTOR_NAME, DEPARTMENT_POSITION, REQUESTOR_TELEPHONE_NUMBER, REQUESTOR_EMAIL, PROJECT_MANAGER_NAME, PROJECT_MANDATE_NUMBER, PROJECT_TITLE, REQUIREMENTS, TIMESCALE)
VALUES ('$check', '$_POST[REQUESTOR_NAME]', '$_POST[DEPARTMENT_POSITION]', '$_POST[REQUESTOR_TELEPHONE_NUMBER]', '$_POST[REQUESTOR_EMAIL]', '$_POST[PROJECT_MANAGER_NAME]', '$_POST[PROJECT_MANDATE_NUMBER]', '$_POST[PROJECT_TITLE]', '$_POST[REQUIREMENTS]', '$_POST[TIMESCALE]')";
#if the data is added to the database echo ref and message otherwise show error.
if ($conn->query($sql) === TRUE) {
echo "Your Request :"$check" has been submitted and you should receive a confirmation email shortly.";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
谢谢大家。
Now on the final php file which adds all the user input to the database I have an include secondfile.php to call the number the user entered so that can be added to the database in a different table than the one it was checked against.
这不是这些东西的工作原理。用户在表单中输入的数据需要从一个请求传输到另一个编码在 url 中。您不能只包含一个源文件(自用户上次访问以来可以调用数百次)并读取用户提交给它的值。用户输入的所有内容都需要保存在某个地方。您可以将其保存在用户的会话中,将其写入数据库或通过 url (以及下一个,下一个,和...)将其传输到下一页。
无法仅通过包含来检索提交给文件 2 的数据。
您应该阅读一些关于 http 和 PHP 的初学者教程,以了解请求的生命周期。
PHP 的生命周期使您无法实现所需。
当你请求表单时,服务器活跃起来,给你表单,然后 "dies"。
当您第一次将数据发送回 2.php
时,脚本一完成,php 就会将重定向发送到浏览器并关闭。
当浏览器获得重定向时,它会调用文件 3,这是另一个生死循环,当您 post 该表单时,它是一个新循环。
重点是,它们之间没有存储状态,当您将文件 2 包含在文件 4 中时,您唯一要做的就是获取文件 2 中的程序,而不是它在处理过程中处理的数据之前的任何运行。
您需要在某种持久层中处理该数据。例如:当您的数据达到 2.php
时,您将该数字存储在数据库或缓存中的临时 table 中。这样当你到达文件 4 时,你就可以从那里读取数据。
请注意,您根本不需要在 4.php
中包含 2.php
,没有意义。
您不能因为具有旧 POST 变量的值而进行包含。
Juste 把你的价值放在用户 SESSION
在您的第二个文件中:
$result = $conn->query($sql);
if ($result->num_rows == 1){
header ("Location: http://www.example.com/3.php");
$_SESSION['REFERENCE_NO']=$check
}
else {echo "ERROR";}
然后在第 4 个文件中将所有“$check”更改为“$_SESSION['REFERENCE_NO']”
希望对您有所帮助
我有 4 个 php 文件,其中 2 个是表单。在第一个表单中,用户输入一个数字并提交第二个 php 文件(表单操作)检查该数字是否存在于 sql 数据库中。如果是,它会将用户重定向到第三个 php 文件,该文件是其余详细信息的表单。现在,在将所有用户输入添加到数据库的最终 php 文件中,我有一个包含 secondfile.php 来调用用户输入的号码,以便可以将其添加到不同 [=37= 的数据库中] 比它被检查的那个。它基本上是 link 两个 table 在一起的方法。我已经尝试了几个测试,但除了这种情况外,我唯一的理论是它失败是因为重定向 header(/var/etc/whatever/thirdfile.php)。任何人都可以告诉我解决它的方法或替代方法吗?
文件 1:
<html>
<body>
<p>Enter the reference number of you request<i>(this should have been sent to you by the sponsor)</i>:</p>
<form action="2.php" method="POST">
<fieldset align="left">
<table>
<tr>
<td>Reference Number:</td><td><input type="text" name="REFERENCE_NO" size="10" /></td>
</tr>
<input type="submit" value="Submit">
</table>
</body>
</html>
文件 2:
<html>
<?php
$check = $_POST["REFERENCE_NO"];
$servername = "x.x.x.x";
$username = "SarahSmith";
$password = "something123";
$dbname = "DB";
#connect to MySQL server
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error){
die ("Connection failed: " . $conn->connect_error);
}
#check reference number exists
$sql ="SELECT REF FROM TEST WHERE REF='$check';";
#check if query returned a row
$result = $conn->query($sql);
if ($result->num_rows == 1){
header ("Location: http://www.example.com/3.php");
}
else {echo "ERROR";}
$conn->close();
?>
</html>
文件 3:
<html>
<form action="4.php" method="POST">
<fieldset align="left">
<legend>Requestor Form</legend>
<p><i>To be completed by the requestor</i></p>
<table>
<tr>
<td>Name:</td><td><input type="text" name="REQUESTOR_NAME" size="40" /> </td>
</tr>
<tr>
<td>Department/Position:</td><td><input type="text" name="DEPARTMENT_POSITION" size="40" /></td>
</tr>
<tr>
<td>Telephone Number:</td><td><input type="text" name="REQUESTOR_TELEPHONE_NUMBER" size="40" maxlength="11" /></td>
</tr>
<tr>
<td>Email Address:</td><td><input type="email" name="REQUESTOR_EMAIL" size="40" /></td>
</tr>
<tr>
<td>Project Manager Name:</td><td><input type="text" name="PROJECT_MANAGER_NAME" size="40" /></td>
</tr>
<tr>
<td>Project Mandate Number:</td><td><input type="text" name="PROJECT_MANDATE_NUMBER" size="40" /></td>
</tr>
<tr>
<td>Project Title:</td><td><input type="text" name="PROJECT_TITLE" size="40" /></td>
</tr>
<tr>
<td>What are the Project requirements:</td><td><textarea placeholder="Please detail exact requirements or contact us on the above e-mail address in order to discuss your requirements prior to submitting your request" cols="42" rows="10" name="REQUIREMENTS" id="REQUIREMENTS" ></textarea></td>
</tr>
<tr>
<td>What are the Project timescales:</td><td><input type="date" name="TIMESCALE" size="40" /></td>
</tr>
<input type="submit" value="Sumbit">
</form>
文件 4:
<?php
$servername = "x.x.x.x";
$username = "SarahSmith";
$password = "something123";
$dbname = "DB";
include_once '2.php';
#connect to MySQL server
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error){
die ("Connection failed: " . $conn->connect_error);
}
#create MySQL query to add new record to TEST table
$sql = "INSERT INTO REQ (REFERENCE_NO, REQUESTOR_NAME, DEPARTMENT_POSITION, REQUESTOR_TELEPHONE_NUMBER, REQUESTOR_EMAIL, PROJECT_MANAGER_NAME, PROJECT_MANDATE_NUMBER, PROJECT_TITLE, REQUIREMENTS, TIMESCALE)
VALUES ('$check', '$_POST[REQUESTOR_NAME]', '$_POST[DEPARTMENT_POSITION]', '$_POST[REQUESTOR_TELEPHONE_NUMBER]', '$_POST[REQUESTOR_EMAIL]', '$_POST[PROJECT_MANAGER_NAME]', '$_POST[PROJECT_MANDATE_NUMBER]', '$_POST[PROJECT_TITLE]', '$_POST[REQUIREMENTS]', '$_POST[TIMESCALE]')";
#if the data is added to the database echo ref and message otherwise show error.
if ($conn->query($sql) === TRUE) {
echo "Your Request :"$check" has been submitted and you should receive a confirmation email shortly.";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
谢谢大家。
Now on the final php file which adds all the user input to the database I have an include secondfile.php to call the number the user entered so that can be added to the database in a different table than the one it was checked against.
这不是这些东西的工作原理。用户在表单中输入的数据需要从一个请求传输到另一个编码在 url 中。您不能只包含一个源文件(自用户上次访问以来可以调用数百次)并读取用户提交给它的值。用户输入的所有内容都需要保存在某个地方。您可以将其保存在用户的会话中,将其写入数据库或通过 url (以及下一个,下一个,和...)将其传输到下一页。
无法仅通过包含来检索提交给文件 2 的数据。
您应该阅读一些关于 http 和 PHP 的初学者教程,以了解请求的生命周期。
PHP 的生命周期使您无法实现所需。
当你请求表单时,服务器活跃起来,给你表单,然后 "dies"。
当您第一次将数据发送回 2.php
时,脚本一完成,php 就会将重定向发送到浏览器并关闭。
当浏览器获得重定向时,它会调用文件 3,这是另一个生死循环,当您 post 该表单时,它是一个新循环。
重点是,它们之间没有存储状态,当您将文件 2 包含在文件 4 中时,您唯一要做的就是获取文件 2 中的程序,而不是它在处理过程中处理的数据之前的任何运行。
您需要在某种持久层中处理该数据。例如:当您的数据达到 2.php
时,您将该数字存储在数据库或缓存中的临时 table 中。这样当你到达文件 4 时,你就可以从那里读取数据。
请注意,您根本不需要在 4.php
中包含 2.php
,没有意义。
您不能因为具有旧 POST 变量的值而进行包含。 Juste 把你的价值放在用户 SESSION
在您的第二个文件中:
$result = $conn->query($sql);
if ($result->num_rows == 1){
header ("Location: http://www.example.com/3.php");
$_SESSION['REFERENCE_NO']=$check
}
else {echo "ERROR";}
然后在第 4 个文件中将所有“$check”更改为“$_SESSION['REFERENCE_NO']”
希望对您有所帮助