Help with arrays

This is the result from a print_r($_POST) before my loop:

Array ( [piccap] => Array ( [0] => up [1] => down ) [photoid] => Array ( [0] => 25 [1] => 83 ) [catid3] => 2 [submit] => Edit )

So, what I am submitting is being posted but the database is only being updated with the second entry.

My loop is only returning the last item.

Editing the first three entries of a category with eight resulted in no change.

But updating the last picture caption worked.

I think it must be my loop. I tried a foreach with no joy but have a funny feeling i should probably be using a foreach within a foreach - a thing I’m clueless as to its implimentation.

This is what I have at the moment


print_r($_POST);	
	$id = $_POST['photoid'];
	
for ($i = 0; $i < count($id); $i++) {
	$pid = $_POST['photoid'];
	$newcaption = $_POST['piccap'];
	
	$sql = "UPDATE gallery_photos SET
			photo_caption='$newcaption[$i]'
			WHERE photo_id='$pid[$i]'";
			
}			
	if (@mysql_query($sql)) {
		echo '<h3 id="message">Update successful.</h3>';
	} else {
		echo '<h3 id="message">Error updating photo caption: ' .
			mysql_error() . '</h3>';
	}

Any help appreciated, thankyou.

Your code to execute the query isn’t within the loop. Move it within the loop.

I thought the working bit was the update part. I moved the curly brace so that i only had one success message, I’ll have to work round that somehow. Thanks, Dan.

The line with the UPDATE is just a string declaration. It puts some text into memory, doesn’t do anything. The mysql_query() call is what actually sends that string to the database server.