You need to make the names of your form elements unique so that they don't clash. One way would be to post them through as arrays instead:
Also, can't you use the same $count
variable to loop over the potential results?
It looks like you're posting the hiddens with the name loop
in order to pass through how many times over the loop you need to go. If not, you can post the loop variable through like this, but you use it in place of $count
for your update loop. Also, I'd consider just adding that element in once, at the end - using the value of $count
instead.
E.g. Something like this may work for you...
<form action='' method='post'>
<?php
for ($i=0; $i<$count; $i++) {
// How is img_id set here? You have no indication in your example - I'll assume that you've put them into an array...
echo "<input type='hidden' name='img_id[\"".$i.\""]' value='".$img_id[$i]."' />";
echo "<input type='text' name='name[".$i."]' />";
}
?>
<input type='submit' value='Update' name='update' /></form>
<?php
if (isset($_POST['update'])) {
for ($i=0; $i<$count; $i++) {
if ( array_key_exists( 'name', $_POST ) && is_array( $_POST['name'] ) && array_key_exists( $i, $_POST['name' ] ) {
$query = "UPDATE photos SET description = '".$_POST['name'][$i]."' WHERE id = '".$_POST['img_id'][]."'";
$res = mysql_query($query) or die(mysql_error);
}
}
}
?>
Or, if you can't use $count
:
<form action='' method='post'>
<?php
echo( "<input type='hidden' name='loop' value='".$count."' />" );
for ($i=0; $i<$count; $i++) {
// How is img_id set here? You have no indication in your example - I'll assume that you've put them into an array...
echo "<input type='hidden' name='img_id[\"".$i.\""]' value='".$img_id[$i]."' />";
echo "<input type='text' name='name[".$i."]' />";
}
?>
<input type='submit' value='Update' name='update' /></form>
<?php
if (isset($_POST['update'])) {
for ($i=0; $i<$_POST['loop']; $i++) {
if ( array_key_exists( 'name', $_POST ) && is_array( $_POST['name'] ) && array_key_exists( $i, $_POST['name'] ) {
$query = "UPDATE photos SET description = '".$_POST['name'][$i]."' WHERE id = '".$_POST['img_id'][$i]."'";
$res = mysql_query($query) or die(mysql_error);
}
}
}
?>