I am trying to add a comment column. But I am getting a similar problem to the one I had before, where when i add a comment 7 other rows are being added also.
I am using,
$comment = (isset($_POST['comment']) && array_key_exists($key,$_POST['comment']) ? $_POST['comment'][$key] : '0');
But I don’t want the comment field to be zero if there is no comment, just leave it empty. Because I am hoping to take this data from the database and put it into a pdf. Therefore i don’t want a ‘0’ for a comment where it should actually be empty. Although I could just tell the function creating the pdf to ignore zeroes in the comment field.
So the only issue i have right now is that multiple rows are being added when I add one comment.
Here are files, notes.html
<form action="?addnotes" method="post">
<input type="submit" name="addnotes" value="Submit">
<table>
<tr>
<th>Course</th>
<th>Student</th>
<th>Absence</th>
<th>Late</th>
<th>Equipment</th>
<th>Effort</th>
<th>Comment</th>
</tr>
<?php $x = 1;?>
<?php foreach (array_reverse($courses) as $course):
foreach (array_reverse($learners) as $learner): ?>
<?php if($course['id']==$learner['courseid']): ?>
<tr>
<td>
<?php htmlout($course['course']) ?>
</td>
<td>
<?php htmlout($learner['learner']);?>
</td>
<td>
<input type="hidden" name="course[<?php echo $x; ?>]" value="<?php htmlout($course['id'])?>">
<input type="hidden" name="student[<?php echo $x; ?>]" value="<?php htmlout($learner['id']);?>">
<input type="radio" name="attend[<?php echo $x; ?>]" value="absence">
</td>
<td>
<input type="radio" name="attend[<?php echo $x; ?>]" value="late">
</td>
<td>
<input type="checkbox" name="equip[<?php echo $x; ?>]" value="1">
</td>
<td>
<select name="effort[<?php echo $x; ?>]">
<option value="0">-</option>
<?php
foreach(range(1,5) as $ef):
echo '<option value="'.$ef.'">'.$ef.'</option><br />'."\r";
endforeach;
?>
</select>
</td>
<td>
<textarea name="comment[<?php echo $x; ?>]" rows="1" cols="40"></textarea>
</td>
</tr>
<?php $x++;?>
<?php endif; ?>
<?php endforeach; ?>
<?php endforeach; ?>
</table>
<input type="submit" name="addnotes" value="Submit">
</form>
and index.php
if (isset($_GET['addnotes']))
{
//echo "<pre>";
//print_r($_POST);
//echo "</pre>";
include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
try
{
foreach($_POST['student'] as $key => $studentID)
{
if(isset($_POST['attend']) && array_key_exists($key,$_POST['attend']) ||
isset($_POST['equip']) && array_key_exists($key,$_POST['equip']) ||
isset($_POST['effort']) && array_key_exists($key,$_POST['effort']) && $_POST['effort'][$key] != 0 ||
isset($_POST['comment']) && array_key_exists($key,$_POST['comment'])):
$studentid = $_POST['student'][$key];
$courseid = $_POST['course'][$key];
$absence = (isset($_POST['attend']) && array_key_exists($key,$_POST['attend']) && $_POST['attend'][$key] == "absence" ? '1' : '0');
$late = (isset($_POST['attend']) && array_key_exists($key,$_POST['attend']) && $_POST['attend'][$key] == "late" ? '1' : '0');
$equip = (isset($_POST['equip']) && array_key_exists($key,$_POST['equip']) ? '1' : '0');
$effort = (isset($_POST['effort']) && array_key_exists($key,$_POST['effort']) ? $_POST['effort'][$key] : '0');
$comment = (isset($_POST['comment']) && array_key_exists($key,$_POST['comment']) ? $_POST['comment'][$key] : '0');
//$comment = $_POST['comment'][$key];
$sql = 'INSERT INTO notes SET
userid = :studentid
, courseid = :courseid
, date = CURDATE()
, time = CURTIME()
, late = :late
, absence = :absence
, effort = :effort
, comment = :comment
, equip = :equip';
$s = $pdo->prepare($sql);
$s->bindParam(':studentid', $studentid);
$s->bindParam(':courseid', $courseid);
$s->bindParam(':absence', $absence);
$s->bindParam(':late', $late);
$s->bindParam(':equip', $equip);
$s->bindParam(':effort', $effort);
$s->bindParam(':comment', $comment);
$s->execute();
endif;
}
}
catch (PDOException $e)
{
$error = 'Error adding submitted daily notes data. Click the back button to continue.';
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
Thanks,
Shane