Updating a database with a loop/array/foreach?

I can get the info from the database fine with this:

$sql2 = "SELECT 1, 2, 3, 4, 5, 6
			FROM ratestable
			ORDER BY id ASC";
			$stmt2 = $db->prepare($sql2);
			$stmt2->execute();
			$e2 = $stmt->fetch();

and display it with this:

 <?php
	  while($e2 = $stmt2->fetch())
	  {
	?>
   <input type="text" name="1" maxlength="10" value="<?php echo $e2['1'] ?>" class="rates" />
   <input type="text" name="2" maxlength="10" value="<?php echo $e2['2'] ?>" class="rates" />
   <input type="text" name="3" maxlength="10" value="<?php echo $e2['3'] ?>" class="rates" />
   <input type="text" name="4" maxlength="10" value="<?php echo $e2['4'] ?>" class="rates" />
   <input type="text" name="5" maxlength="10" value="<?php echo $e2['5'] ?>" class="rates" />
   <input type="text" name="6" maxlength="10" value="<?php echo $e2['6'] ?>" class="rates" />
   <?php
      }
   ?>

How do I update it to the database?


THIS IS WHERE I AM STUCK....

$sql = "UPDATE ratestable
					SET dates=?, night=?, week=?, month=?, min=?, rank=? 
					WHERE id=?";
			$stmt = $db->prepare($sql);
			$stmt->execute(array(
								$_POST['dates'],
								$_POST['night'],
								$_POST['week'],
								$_POST['month'][,
								$_POST['min'],
								$_POST['rank'],
								$_POST['id'],
								));
			$stmt->closeCursor();

THIS IS WHERE I AM STUCK....

and that’s as far as I can get, Do I need to use an array/loop or foreach and how do I implement it???

updated the code to this:

$sql2 = "SELECT id, 1, 2, 3, 4, 5, 6
			FROM ratestable
			ORDER BY id ASC";
			$stmt2 = $db->prepare($sql2);
			$stmt2->execute();
			$e2 = $stmt->fetch();
<?php
	  while($e2 = $stmt2->fetch())
	  {
	?>
<input type="hidden" name="id" value="<?php echo $e2['id']; ?>" />
   <input type="text" name="1" maxlength="10" value="<?php echo $e2['1'] ?>" class="rates" />
   <input type="text" name="2" maxlength="10" value="<?php echo $e2['2'] ?>" class="rates" />
   <input type="text" name="3" maxlength="10" value="<?php echo $e2['3'] ?>" class="rates" />
   <input type="text" name="4" maxlength="10" value="<?php echo $e2['4'] ?>" class="rates" />
   <input type="text" name="5" maxlength="10" value="<?php echo $e2['5'] ?>" class="rates" />
   <input type="text" name="6" maxlength="10" value="<?php echo $e2['6'] ?>" class="rates" />
   <?php
      }
   ?>

Can get it to update the last last ID but not all rows:

$sql = "UPDATE ratestable
					SET 1=?, 2=?, 3=?, 4=?, 5=?, 6=? 
					WHERE id=?";
			$stmt = $db->prepare($sql);
			$stmt->execute(array(
								$_POST['1'],
								$_POST['2'],
								$_POST['3'],
								$_POST['4'],
								$_POST['5'],
								$_POST['6'],
								$_POST['id'],
								));
			$stmt->closeCursor();

You are putting multiple form fields with the same name in the same form which causes only the last values to get submitted. Get rid of the hidden inputs, then change your text input field name using this structure:

<input type="text" name="rate[<?php echo $e2['id']; ?>][1]" maxlength="10" value="<?php echo $e2['1'] ?>" class="rates" />

a var dump of the resulting $_POST info should show you how the array is formed and how you can loop through it to get what you need.

Awesome thanks that really helped!

Here is the working code:

<input type="text" name="dates[<?php echo $e2['id']; ?>]" maxlength="20" value="<?php echo $e2['dates']; ?>" class="rates" />
   <input type="text" name="night[<?php echo $e2['id']; ?>]" maxlength="20" value="<?php echo $e2['night']; ?>" class="rates" />
   <input type="text" name="week[<?php echo $e2['id']; ?>]" maxlength="20" value="<?php echo $e2['week']; ?>" class="rates" />
   <input type="text" name="month[<?php echo $e2['id']; ?>]" maxlength="20" value="<?php echo $e2['month']; ?>" class="rates" />
   <input type="text" name="min[<?php echo $e2['id']; ?>]" maxlength="20" value="<?php echo $e2['min']; ?>" class="rates" />
   <input type="text" name="rank[<?php echo $e2['id']; ?>]" maxlength="20" value="<?php echo $e2['rank']; ?>" class="rates" />
$sql3 = "UPDATE ratestable
					SET dates=?, night=?, week=?, month=?, min=?, rank=? 
					WHERE id=?";
			$stmt3 = $db->prepare($sql3);
			if(count($_POST['rank']) > 0)
			{
			  foreach($_POST['rank'] AS $key => $val)
			  {
				  $stmt3->execute(array(
									   $_POST['dates'][$key],
									   $_POST['night'][$key],
									   $_POST['week'][$key],
									   $_POST['month'][$key],
									   $_POST['min'][$key],
									   $val, 
									   $key 
									   
									   ));
			  }}
			$stmt3->closeCursor();

glad you got it sorted - cheers