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.
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
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.
That's not how any of these things work. The data the user entered in the forms needs to be transported from one request to the other encoded in the url. You can't just include a source file (which can be called hundreds of times since the user accessed it last times) and read a value that the user submitted to it. Everything the user enters needs to be persisted somewhere. You can save it in the user's session, write it to the database or transport it to the next page via the url (and the next, and the next, and... ).
There's no way to retrieve data that was submitted to file 2 just by including it.
You should read some beginners tutorials on http and PHP to get an idea of the lifecycle of an request.
PHP's lifecycle is the thing that is making what you need impossible. When you request the form, the server comes alive, gives you the form, then "dies". When you send the data back for the first time to 2.php
, as soon as the script finishes, the php sends the redirect to the browser and closes. When the browser gets the redirect, it calls for file 3, which is another become alive-die cycle, and when you post that form, it's a new cycle.
The point is, that there is no state stored between these, and when you include file 2 in file 4, the only thing you do is acquire the program that is in file 2, but not the data it handled during any previous runs.
You need to take care of that data in some kind of persistence layer. For example: when your data gets to 2.php
you store that number in a temp table in the database, or in cache. That way when you get to file 4, then you can read the data from there.
Note, that you don't need to include 2.php
in 4.php
at all, there is no point.