Jump to content

Recommended Posts

I have a form, it's reading the mysql database. I have a while loop to display all of the fields in the table so you can edit it. Yes, it displays the fields, but I'm having problems with the update part, when I specify the ID (which is a hidden field) it takes the last field in the form and uses that to update. 

For example:


UPDATE mitigations SET `status`='Watch', `notes`='test6' WHERE id='6';UPDATE mitigations SET `status`='Watch', `notes`='test6' WHERE id='6';UPDATE mitigations SET `status`='Watch', `notes`='test6' WHERE id='6';UPDATE mitigations SET `status`='Watch', `notes`='test6' WHERE id='6';UPDATE mitigations SET `status`='Watch', `notes`='test6' WHERE id='6';UPDATE mitigations SET `status`='Watch', `notes`='test6' WHERE id='6';

I need id to actually get the ID from the form where it's a hidden field. It works now, but only for the last field. Make sense?

 

Here's the actual code I have on the edit form itself:

<?php while ($data = mysql_fetch_array($mitigation_query)) {echo "<form><tr><td width='56' height='20px'><input type=hidden name='mitigation_id' value=". $data['id'] ."><span id='MitigationOnTrack'><input type='text' name='mitigation_status' value=". $data['status'] ."></span></td><td width='758'><input type=text name='mitigation_id' value=". $data['id'] ."><textarea rows=5 cols=100 name='mitigation_notes'>" . $data['notes'] ."</textarea></td></tr></form>";
 }
?>
Edited by Eccentricson
Link to comment
https://forums.phpfreaks.com/topic/283242-php-form-submissions-for-while-loops/
Share on other sites

 

 

when you rightclick > view source, do you see correct ids or are they all outputting the same (last) id? where is your submit button in relation to this? What code actually submits the form? My *guess* is that since you don't have a submit button in each of the forms, you must have a single submit button and js doing something to submit. So the problem is likely somewhere in the js

when you rightclick > view source, do you see correct ids or are they all outputting the same (last) id? where is your submit button in relation to this? What code actually submits the form? My *guess* is that since you don't have a submit button in each of the forms, you must have a single submit button and js doing something to submit. So the problem is likely somewhere in the js

Yes, the IDs are correct for each row it spits out.  The submit button is in the same form tag.  I have a form in the beginning, then this form at the while loop and I close both at the end of the file.  I don't have any JS associated either.

Are you wanting to update multiple records at the same time? You need to setup you form like this

echo '<form action="edit_page.php" method="post">
<table>
 	<tr>
 		<th>Status</th>
 		<th>Notes</th>
 	</tr>';
while ($data = mysql_fetch_array($mitigation_query)) {
	$row_id = $data['id'];
	echo "
	<tr>
		<td width='56' height='20px'>
			<span id='MitigationOnTrack'><input type='text' name='mitigation[$row_id][status]' value=". $data['status'] ."></span>
		</td>
		<td width='758'>
			<input type=text name='mitigation[$row_id][id]' value=". $data['id'] .">
			<textarea rows=5 cols=100 name='mitigation[$row_id][notes]'>" . $data['notes'] ."</textarea>
		</td>
	</tr>";
}

echo '
	<tr>
		<td colspan="2"><input type="submit" name="update" value="Update" /></td> <!-- one update field -->
	</tr>
</table>
</form>';

Then to update the records you'd use

if(isset($_POST['update']))
{
        // loop through the form entries
	foreach($_POST['mitigation'] as $row_id => $mitigation)
	{
		$status = mysql_real_escape_string($mitigation['status']);
		$notes  = mysql_real_escape_string($mitigation['notes']);

		$row_id = (int) $row_id;

                // update the row
		mysql_query("UPDATE mitigations SET `status`='$status', `notes`='$notes' WHERE id='$row_id'");
	}
}
Edited by Ch0cu3r

okay well if the correct ids are being output then i don't think this is a php problem.

 

can you please clarify or post the view-source? because at a minimum you are definitely doing something wrong with those form tags wrapped around each set of elements.

Here's the sourcecode:

 <tr><td width='56' height='20px'><input type=hidden name='mitigation_id' value=1><span id='MitigationOnTrack'><input type='text' name='mitigation_status' value=Watch></span></td><td width='758'><input type=text name='mitigation_id' value=1><textarea rows=5 cols=100 name='mitigation_notes'></textarea></td></tr><tr><td width='56' height='20px'><input type=hidden name='mitigation_id' value=2><span id='MitigationOnTrack'><input type='text' name='mitigation_status' value=Watch></span></td><td width='758'><input type=text name='mitigation_id' value=2><textarea rows=5 cols=100 name='mitigation_notes'></textarea></td></tr><tr><td width='56' height='20px'><input type=hidden name='mitigation_id' value=3><span id='MitigationOnTrack'><input type='text' name='mitigation_status' value=Watch></span></td><td width='758'><input type=text name='mitigation_id' value=3><textarea rows=5 cols=100 name='mitigation_notes'></textarea></td></tr><tr><td width='56' height='20px'><input type=hidden name='mitigation_id' value=4><span id='MitigationOnTrack'><input type='text' name='mitigation_status' value=Watch></span></td><td width='758'><input type=text name='mitigation_id' value=4><textarea rows=5 cols=100 name='mitigation_notes'></textarea></td></tr><tr><td width='56' height='20px'><input type=hidden name='mitigation_id' value=5><span id='MitigationOnTrack'><input type='text' name='mitigation_status' value=Watch></span></td><td width='758'><input type=text name='mitigation_id' value=5><textarea rows=5 cols=100 name='mitigation_notes'></textarea></td></tr><tr><td width='56' height='20px'><input type=hidden name='mitigation_id' value=6><span id='MitigationOnTrack'><input type='text' name='mitigation_status' value=Watch></span></td><td width='758'><input type=text name='mitigation_id' value=6><textarea rows=5 cols=100 name='mitigation_notes'></textarea></td></tr> 

basically what i'm getting at here is that your form is improperly structured. You wrap your grouped elements in form tags that point to nowhere and have no submit buttons within them. So how does one actually submit the changes? And if you aren't using javascript to grab that stuff.. well already I know one problem is that your submit button (assuming it exists - i have yet to see it) can't possibly be associated with that stuff.

 

But I can't tell you how to fix it unless I see the full picture. But in general, the solution is going to look something along the lines of what Ch0cu3r showed. For starters, notice how he only outputs a single form tag wrapped around the entire thing.

basically what i'm getting at here is that your form is improperly structured. You wrap your grouped elements in form tags that point to nowhere and have no submit buttons within them. So how does one actually submit the changes? And if you aren't using javascript to grab that stuff.. well already I know one problem is that your submit button (assuming it exists - i have yet to see it) can't possibly be associated with that stuff.

 

But I can't tell you how to fix it unless I see the full picture. But in general, the solution is going to look something along the lines of what Ch0cu3r showed. For starters, notice how he only outputs a single form tag wrapped around the entire thing.

Unfortunately, due to the sensitive data, I can't post the source code, without significant data scrubbing.  I have changed it have one single form, I have figured the best way to fix this, is to use variables for the names of the fields.  Simply because the last ID in the database was setting the data for that name and the form was using the same data over and over until the while loop ended.  If this makes sense.  Your guys help has helped me a lot, I really appreciate it!!  If I was making any money on this project, I'd donate. :)

 

Are you wanting to update multiple records at the same time? You need to setup you form like this

echo '<form action="edit_page.php" method="post">
<table>
 	<tr>
 		<th>Status</th>
 		<th>Notes</th>
 	</tr>';
while ($data = mysql_fetch_array($mitigation_query)) {
	$row_id = $data['id'];
	echo "
	<tr>
		<td width='56' height='20px'>
			<span id='MitigationOnTrack'><input type='text' name='mitigation[$row_id][status]' value=". $data['status'] ."></span>
		</td>
		<td width='758'>
			<input type=text name='mitigation[$row_id][id]' value=". $data['id'] .">
			<textarea rows=5 cols=100 name='mitigation[$row_id][notes]'>" . $data['notes'] ."</textarea>
		</td>
	</tr>";
}

echo '
	<tr>
		<td colspan="2"><input type="submit" name="update" value="Update" /></td> <!-- one update field -->
	</tr>
</table>
</form>';

Then to update the records you'd use

if(isset($_POST['update']))
{
        // loop through the form entries
	foreach($_POST['mitigation'] as $row_id => $mitigation)
	{
		$status = mysql_real_escape_string($mitigation['status']);
		$notes  = mysql_real_escape_string($mitigation['notes']);

		$row_id = (int) $row_id;

                // update the row
		mysql_query("UPDATE mitigations SET `status`='$status', `notes`='$notes' WHERE id='$row_id'");
	}
}

This worked with a few tweaks to it.  Thanks so much!!!  I appreciate it!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.