SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

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

    Code:
    $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:

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

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

  2. #2
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    updated the code to this:

    Code:
    $sql2 = "SELECT id, 1, 2, 3, 4, 5, 6
    			FROM ratestable
    			ORDER BY id ASC";
    			$stmt2 = $db->prepare($sql2);
    			$stmt2->execute();
    			$e2 = $stmt->fetch();
    Code:
    <?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:

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

  3. #3
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    HTML Code:
    <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.
    aaron-fisher.com - PHP articles and more

  4. #4
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aamonkey View Post
    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:

    HTML Code:
    <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:

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

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

  5. #5
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    glad you got it sorted - cheers
    aaron-fisher.com - PHP articles and more


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •