Reputation: 59
I am pulling data from the database and displaying in a form.
I want the values to be edited and then the form when resubmitted will update the data in the table based on the corresponding row, however its updating the table based on the index value 4 only. All other data for the teamid
is being replaced with the data in the arrays for the 4
index.
Here is the var_dump
when the form is submitted ( this shows the modified data so its submitted correctly so assume its an issue with the loop )
array (size=12)
'teamid' => string '1' (length=1)
'activity' => string 'Mill 1' (length=6)
'activity2' => string 'Enter Details' (length=13)
'info' => string 'Auto process' (length=12)
'info2' => string 'Enter Details' (length=13)
'wsno' => string '1' (length=1)
'labour' =>
array (size=5)
0 => string 'Admin' (length=5)
1 => string 'QC Manager' (length=10)
2 => string 'Supervisor' (length=10)
3 => string 'Team Leaders' (length=12)
4 => string 'Line Operators' (length=14)
'hours' =>
array (size=5)
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string '2' (length=1)
3 => string '8' (length=1)
4 => string '8' (length=1)
'noworkers' =>
array (size=5)
0 => string '2' (length=1) << CHANGED THIS FROM 1 TO 2
1 => string '1' (length=1)
2 => string '1' (length=1)
3 => string '1' (length=1)
4 => string '3' (length=1)
'rateperhour' =>
array (size=5)
0 => string '11.00' (length=5)
1 => string '10.00' (length=5)
2 => string '9.00' (length=4)
3 => string '8.50' (length=4)
4 => string '8.00' (length=4)
'totalcost' =>
array (size=5)
0 => string '22' (length=2) << THIS IS THE NEW CALCULATED VALUE
1 => string '10.00' (length=5)
2 => string '18.00' (length=5)
3 => string '68.00' (length=5)
4 => string '192.00' (length=6)
'addteam' => string '' (length=0)
The value I changes was on the first index (0
) -as No Workers to 2 and the value is correct when submitted
if(!isset($_GET['team'])) {
$teamid = 1;
} else {
$teamid = $_GET['team'];
}
$teamlq = mysqli_query($dbc,"SELECT * FROM `teams` WHERE `teamid` = '$teamid'");
$teamlr = mysqli_fetch_assoc($teamlq);
$actq = mysqli_query($dbc,"SELECT * FROM `teams` GROUP BY `activity` ASC");
$act = mysqli_fetch_assoc($actq);
$infoq = mysqli_query($dbc,"SELECT * FROM `teams` GROUP BY `info` ASC");
$info = mysqli_fetch_assoc($infoq);
$wsq = mysqli_query($dbc,"SELECT * FROM `workstation_costing` GROUP BY `wsno` ASC");
$ws = mysqli_fetch_assoc($wsq);
if(isset($_POST['addteam'])) {
$labour[] = $_POST['labour'];
$hours[] = $_POST['hours'];
$noworkers[] = $_POST['noworkers'];
$rateperhour[] = $_POST['rateperhour'];
$totalcost[] = $_POST['totalcost'];
$maxrows = 5;
$i = 0;
$info = $_POST['info'];
if($_POST['info'] == 'other') {
$info = $_POST['info2'];
}
$activity = $_POST['activity'];
if($_POST['activity'] == 'other'){
$activity = $_POST['activity2'];
}
echo "<pre>";
var_dump($_POST);
echo "</pre>";
do {
$labour = $_POST['labour'][$i];
$rateperhour = $_POST['rateperhour'][$i];
$hours = $_POST['hours'][$i];
$noworkers = $_POST['noworkers'][$i];
$totalcost = $_POST['totalcost'][$i];
$insert = mysqli_query($dbc,"UPDATE `teams` SET `activity` = '$activity',`info` = '$info',`wsno` = '$_POST[wsno]',`labour` = '$labour',`rateperhour`='$rateperhour',`hrsengaged` = '$hours',`noworkers`='$noworkers',`totalcost`='$totalcost' WHERE `teamid` = '$_POST[teamid]'");
echo mysqli_error($dbc);
$i++;
}while ($i < $maxrows);
}
Here is a screenshot of the table showing teamid
1 which the form is updating
( before form submission )
Upvotes: 1
Views: 48
Reputation: 11642
Notice that inside the loop you do:
$insert = mysqli_query($dbc,"UPDATE `teams` SET `activity` = '$activity',`info` = '$info',`wsno` = '$_POST[wsno]',`labour` = '$labour',`rateperhour`='$rateperhour',`hrsengaged` = '$hours',`noworkers`='$noworkers',`totalcost`='$totalcost' WHERE `teamid` = '$_POST[teamid]'");
but you never update the $_POST[teamid]
so in each iteration you update all the fields with that $_POST[teamid]
with the current data of the loop -> this cause to the table has only the last loop values for all the row with that teamid
.
In order to overcome that you can add ID
column to your table and in the loop update the row by its ID
rather then the teamid
-> this will result in updating 1 row at each iteration
Upvotes: 1