dongni8969 2015-01-27 09:35
浏览 55
已采纳

PHP重定向导致包括失败

I have 4 php files 2 of which are forms. On the first form the user enters a number and on submit the second php file (form action) checks that the number exists in the sql database. If it does it redirects the user to the third php file which is a form for the rest of the details. 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. It basically is a way to link the two tables together. I have tried several tests which work except this case and my only theory is that it fails because of the redirect header(/var/etc/whatever/thirdfile.php). Can anyone tell me a way to get around it or an alternative way of doing it?

File 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>

File 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>

File 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>

File 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();
?>

Thanks guys.

  • 写回答

3条回答 默认 最新

  • dongsha1544 2015-01-27 09:49
    关注

    You can't make an include for having the value of your old POST var. Juste put your value in the user SESSION

    In your 2nd file :

    $result = $conn->query($sql);
    if ($result->num_rows == 1){
    header ("Location: http://www.example.com/3.php");
    $_SESSION['REFERENCE_NO']=$check
    }
    else {echo "ERROR";}
    

    And in the 4th file change all your "$check" by "$_SESSION['REFERENCE_NO']"

    wish it will help you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作