evibesmusic
asked on
Query is updating my DB using only the last row of data in a dynamically created form?
Experts,
I have a dynamically created form which contains a table. The rows of this table contain information pulled from the database. Each row of the table has been given a unique id using the 'ID' filed contained within the DB. Additionally, each row of the table contains two dropdown menus which I would like my users to be able to update.
The problem is that when processing this form via a foreach loop, the database is only updated using the very last row of data and whatever has been selected using this row's dropdown menus.
Therefore, changes that I may make to row 5 are overwritten by the changes in the last row, row 7 upon form processing.
Here is my code which identifies via a foreach loop, the ID of each row, and attempts to process the dropdown data from each row.
Any suggestions on how I can straighten this out? Cheers!
This is an abbreviate version of my form:
echo'<td>';
echo'<select name="expected" id="expected" style="font-size:10px">';
echo'<option value="'.$result3['expecte d'].'">'.$ result3['e xpected']. '</option> ';
echo'<option value="Yet to Discuss">Yet to Discuss</option>';
echo'<option value="Undecided">Undecide d</option> ';
echo'<option value="Plans to Work">Plans to Work</option>';
echo'<option value="Will Not be Working">Will Not be Working</option>';
echo'</select>';
echo'</td>';
echo'<td>';
echo'<select name="coding" id="coding" style="font-size:10px">';
echo'<option value="'.$result3['coding' ].'">'.$re sult3['cod ing'].'</o ption>';
echo'<option value="LOA">LOA</option>';
echo'<option value="Sick">Sick</option> ';
echo'<option value="Sick - CESLA">Sick - CESLA</option>';
echo'<option value="Sick - FMLA">Sick - FMLA</option>';
echo'<option value="No Call No Show">No Call No Show</option>';
echo'<option value="Worked">Worked</opt ion>';
echo'</select>';
echo'</td>';
echo'<td><textarea name="notes" id="notes" cols="15" rows="1" style="font-size:10px;">'. $result3[' notes'].'< /textarea> </td>';
echo'<input name="row_number[]" type="hidden" value="'.$result3['ID'].'" />';
This is the portion of my form processing script which should save the data to each row of the database based on the unique ID for each row of the table:
//THE NAME OF DROPDOWN 1
$expected = $_POST['expected'];
//THE NAME OF DROPDOWN 2
$coding = $_POST['coding'];
//THE NAME OF TEXTFIELD 1
$notes = $_POST['notes'];
//THE UNIQUE ID OF EACH TABLE ROW
$row_number = $_POST[row_number];
foreach($_POST[row_number] as $row_number){
$update_row = "UPDATE schedule SET expected='$expected', coding='$coding', notes='$notes' WHERE ID='$row_number'";
$update_row_query = mysql_query($update_row) or die ("Could not run query: ".$update_row."<br />\n".mysql_error());
echo $update_row;
}
I have a dynamically created form which contains a table. The rows of this table contain information pulled from the database. Each row of the table has been given a unique id using the 'ID' filed contained within the DB. Additionally, each row of the table contains two dropdown menus which I would like my users to be able to update.
The problem is that when processing this form via a foreach loop, the database is only updated using the very last row of data and whatever has been selected using this row's dropdown menus.
Therefore, changes that I may make to row 5 are overwritten by the changes in the last row, row 7 upon form processing.
Here is my code which identifies via a foreach loop, the ID of each row, and attempts to process the dropdown data from each row.
Any suggestions on how I can straighten this out? Cheers!
This is an abbreviate version of my form:
echo'<td>';
echo'<select name="expected" id="expected" style="font-size:10px">';
echo'<option value="'.$result3['expecte
echo'<option value="Yet to Discuss">Yet to Discuss</option>';
echo'<option value="Undecided">Undecide
echo'<option value="Plans to Work">Plans to Work</option>';
echo'<option value="Will Not be Working">Will Not be Working</option>';
echo'</select>';
echo'</td>';
echo'<td>';
echo'<select name="coding" id="coding" style="font-size:10px">';
echo'<option value="'.$result3['coding'
echo'<option value="LOA">LOA</option>';
echo'<option value="Sick">Sick</option>
echo'<option value="Sick - CESLA">Sick - CESLA</option>';
echo'<option value="Sick - FMLA">Sick - FMLA</option>';
echo'<option value="No Call No Show">No Call No Show</option>';
echo'<option value="Worked">Worked</opt
echo'</select>';
echo'</td>';
echo'<td><textarea name="notes" id="notes" cols="15" rows="1" style="font-size:10px;">'.
echo'<input name="row_number[]" type="hidden" value="'.$result3['ID'].'"
This is the portion of my form processing script which should save the data to each row of the database based on the unique ID for each row of the table:
//THE NAME OF DROPDOWN 1
$expected = $_POST['expected'];
//THE NAME OF DROPDOWN 2
$coding = $_POST['coding'];
//THE NAME OF TEXTFIELD 1
$notes = $_POST['notes'];
//THE UNIQUE ID OF EACH TABLE ROW
$row_number = $_POST[row_number];
foreach($_POST[row_number]
$update_row = "UPDATE schedule SET expected='$expected', coding='$coding', notes='$notes' WHERE ID='$row_number'";
$update_row_query = mysql_query($update_row) or die ("Could not run query: ".$update_row."<br />\n".mysql_error());
echo $update_row;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@PranjalShah:
If the following: $update_row = "UPDATE schedule SET expected='".$_post['expect ed'.$row_n umber]."' WHERE ID='".$row_number."'"
Supposed to be: $update_row = "UPDATE schedule SET expected='".$_POST['expect ed'.$row_n umber]."' WHERE ID='".$row_number."'"
If the following: $update_row = "UPDATE schedule SET expected='".$_post['expect
Supposed to be: $update_row = "UPDATE schedule SET expected='".$_POST['expect
yes, the $_POST should be in uppercase. And try echoing your queries before executing it so that you get the idea of what is being posted.
ASKER
@PranjalShah:
Thank you very much. Your solution worked. Here come the points.
Thank you very much. Your solution worked. Here come the points.
ASKER
Thanks!
Open in new window
then your for loop
Open in new window