I have a couple of pages in my application. In the first page (create_session.php) which is below the user submits the form and goes onto the next page (QandATable.php).
<form action="QandATable.php" method="post" id="sessionForm">
<p>
<strong>
Your Session ID:
</strong>
<span id="idFont">
<?php echo $id; ?>
</span>
</p>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<p>
<strong>
Date:
</strong>
<input type="text" id="datepicker" name="dateChosen" readonly="readonly" />
<p>
<strong>
Start Time:
</strong>
<input type="text" id="timepicker" name="timeChosen" readonly="readonly" />
<p>
<input class="questionBtn" type="submit" value="Prepare Questions" name="prequestion"/>
</p>
</form>
Now if $_POST['id'] equals 1, then there is only 1 exam, but if it it is more than 1, then there are multiple exams which is where the problem occurs.
Lets say I wanted 2 exams and I submit the form to the QandATable.php, then the first exam has no problems, but after I finish the first exam and then submit the form (This form would submit to itself), then for the second exam, I suddenly get notices for all of the $_POST I have in the code below.
<?php
session_start();
if (isset($_POST['id']))
{
$_SESSION['id'] = $_POST['id'];
}
if (isset($_POST['dateChosen']))
{
$_SESSION['dateChosen'] = $_POST['dateChosen'];
}
if (isset($_POST['timeChosen']))
{
$_SESSION['timeChosen'] = $_POST['timeChosen'];
}
$username="xxx";
$password="xxx";
$database="xxx";
mysql_connect('localhost',$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$insert = array();
for ($i = 1, $n = $_POST['sessionNum']; $i <= $n; ++$i)
{
$insert[] = "' ". mysql_real_escape_string( $_POST['id'] ) . ($n == 1 ? '' : $i) . "',' ". mysql_real_escape_string( $_POST['timeChosen'] ) . "',' ". mysql_real_escape_string( date("Y-m-d", strtotime( $_POST['dateChosen'] ) ) ) . "'";
}
$sql = "INSERT INTO Session (SessionId, SessionTime, SessionDate)
VALUES (" . implode('), (', $insert) . ")";
mysql_query($sql);
mysql_close();
?>
So what I want to know is that when I have multiple exams, why am I getting notices on all of the $_POST's after the first exam has been completed? All the notices are undefined indexes on all the posts.
You're getting undefined index
errors because when you view the page without posting a form, the $_POST
data you're looking for does not exist.
Check if the post data exists before trying to use it:
if(isset($_POST['sessionNum'], $_POST['id'], $_POST['timeChosen'], $_POST['dateChosen']))
{
$insert = array();
for ($i = 1, $n = $_POST['sessionNum']; $i <= $n; ++$i)
{
$insert[] = "' ". mysql_real_escape_string( $_POST['id'] ) . ($n == 1 ? '' : $i) . "',' ". mysql_real_escape_string( $_POST['timeChosen'] ) . "',' ". mysql_real_escape_string( date("Y-m-d", strtotime( $_POST['dateChosen'] ) ) ) . "'";
}
$sql = "INSERT INTO Session (SessionId, SessionTime, SessionDate)
VALUES (" . implode('), (', $insert) . ")";
mysql_query($sql);
}
Or use if($_SERVER['REQUEST_METHOD'] == 'POST')
if you know that the correct post data will always be present on a POST request.