Forum Moderators: coopster

Message Too Old, No Replies

Could not update database table with php

         

appletea

2:26 pm on Jun 27, 2010 (gmt 0)

10+ Year Member



Hi,

I need help with updating my database.Currently my code could only update the last member attendance but not the rest of the member.Could anyone spot the mistakes of why it is only updating the last member attendance but not the rest of the member attendance. Pardon the messiness of my code.
Thanks.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<body>

<table border='1'>
<tr>
<th>Participant Name</th>
<th>Class</th>
<th>Day 1</th>
<th>Day 2</th>
<th>Day 3</th>
</tr>


<?php
$HOST = 'localhost';
$USERNAME = 'root';
$PASSWORD = '';
$DB = 'sjas';
$link = mysqli_connect($HOST,$USERNAME,$PASSWORD,$DB) or die (mysqli_connect_error());
$city = "SELECT m.idMember, m.name AS member_name, a.Member_idMember, cl.Course_idCourse, co.idCourse, co.name, cl.idClass
FROM member m, attendance a, Class cl, Course co
WHERE m.idMember = a.Member_idMember AND
a.Class_idClass = cl.idClass AND
cl.Course_idCourse = co.idCourse";

$result = mysqli_query($link,$city) or die(mysqli_error($link));
?>


<?php
while($row = mysqli_fetch_array($result))
{
echo "<td>".$row['member_name']."</td>";
echo "<td>".$row['idClass']."</td>";
echo '<form action="dologin.php" method="post">
<td> <select name="day_present01" >
<option value="0">Absent</option>
<option value="1">Present</option>
</select>
</td>
<td> <select name="day_present02">
<option value="0">Absent</option>
<option value="1">Present</option>
</select>
</td>
<td> <select name="day_present03">
<option value="0">Absent</option>
<option value="1">Present</option>
</select>
<input type="hidden" name = "member_id" value = "'.$row['idMember'].'" />
<input type="hidden" name = "class_id" value = "'.$row['idClass'].'" />
</td>';
echo "</tr>";
}
echo "</table>";
?>

<input type="submit" name="Submit" value="Submit">


</form>
</body>
</html>


--------------dologin.php------------------


<?php
$day_present01 = $_POST['day_present01'];
$day_present02 = $_POST['day_present02'];
$day_present03 = $_POST['day_present03'];
$member_id = $_POST['member_id'];
$class_id = $_POST['class_id'];
$absent=0;
$present=1;


?>
<?php
if ($absent == 0)
{$absent="Absent";
}
else{
$present=="Present";}
?>



<?php
$HOST = 'localhost';
$USERNAME = 'root';
$PASSWORD = '';
$DB = 'sjas';
$link = mysqli_connect($HOST,$USERNAME,$PASSWORD,$DB) or die (mysqli_connect_error());
$course ="UPDATE attendance SET day_present01 ='$day_present01',day_present02 ='$day_present02',day_present03 ='$day_present03' WHERE Member_idMember = '$member_id'AND Class_idClass ='$class_id'";
$result = mysqli_query($link,$course) or die(mysqli_error($link));
if($result){
$statusMessage = '<p>Attendance submitted successfully.<br /><a href="view.php">View Attendance</a></p>';
}else{
$statusMessage = '<p class="error">Attendance could not be submitted. Please try again.<a href="login.php">Back</a>';
}
echo $statusMessage;

?>

rocknbil

7:42 pm on Jun 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



appletea: this is the same problem you had in this thread [webmasterworld.com]. You have multiple occurrences of "whatever_01". Look:


while($row = mysqli_fetch_array($result)) {
// etc., then
<select name="day_present01" >
}

So view the source of your code when this form loads. You will see you have the same number of "day_present01" named select lists as the number of records.

Then when you go to UPDATE, you only do it ONCE.

$course ="UPDATE attendance SET day_present01 ='$day_present01' .....

You see? You need to loop through there too. If there are multiple "days" in the form, you need to update multiple records - otherwise it's going to update all the records to the same value.

I explained this in the other thread - in short, you need to make these form names all unique (or an array,) like

while($row = mysqli_fetch_array($result)) {
// etc., then
$record_id=$row['id'];
$day_one = 'day_present01_id_' . $record_id;
echo "<select name=\"$day_one\">";
// etc.
}

then when you update,


foreach ($_POST as $key=>$value) {
if (preg_match('/present01\_id\_\d$/',$key)) {
list ($d,$pres,$idtag,$record_id) = explode('_',$key);
$day_one = $key;
$day_two = 'day_present02_id_' . $id;
$day_three = 'day_present03_id_' . $id;
$course ="UPDATE attendance SET day_present01 ='$value'," .
"day_present02 ='$_POST[$day_two]',day_present03 ='$_POST[$day_three]'" .
"where id='$record_id'";
// do your update
} // end if
} // End for loop
// NOW output success


Note how I "locked" the form output AND the update to the record id, simplifying the possiblity of updating the wrong record that matches on "class and member id." it also needs to lock to the day of the record, right?

The previous is not working code but you need to get a grasp of what's going wrong here, your forms have multiple values with the same name and an added problem of only making one update.

appletea

4:07 pm on Jun 28, 2010 (gmt 0)

10+ Year Member



Hi rocknbill,

Thanks for your help and reply. As you've stated,the form names need to be unique/ be an array. I've tried using your suggestion and have managed to resolve this issue by using an array instead.Thanks for your suggestion.