Page 1 of 1

updating a database with a loop/array/foreach ??

Posted: Thu Mar 24, 2011 8:01 am
by happypete
I can get the info from the database fine with this:

Code: Select all

$sql2 = "SELECT id, 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:

Code: Select all

 <?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
      }
   ?>

How do I update it to the database?

Code: Select all

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???????

Re: updating a database with a loop/array/foreach ??

Posted: Thu Mar 24, 2011 2:38 pm
by happypete
sorted it :)

Here is the working code:

Code: Select all

<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" />

Code: Select all

$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();