Jump to content

Update multiple rows with same variable


drewb29693

Recommended Posts

I am runnig the fetch array and everything is working great.  What i am having trouble with is when i go to update a row on another page, it is only grabbing the last $POST_['goal_id'] because i don't know how to store ever one that passes through that while loop.  if i could just assign each post that it runs through in the the loop to a variable that would be great, but i am new at this so i don't know how.  Any help would be great.

 

 

<?php 

$line = 1;

while($row1 = mysql_fetch_array($result)){ ?>
     
     <tr>
       <td width="124" height="40" align="right" valign="top" scope="col"><?php echo "Goal ". $line++ . ":";?><span
         class="style1">__</span>     </td>
       
<td width="408" height="40" valign="top" scope="col"><?php echo $row1['goal']; ?></td>
        
       <input type="hidden" readonly="readonly" name="goal_id" id="goal_id" value="<?php echo $row1['goal_id'];?>"      
          size="12"/>  
     <?php } ?>

Link to comment
https://forums.phpfreaks.com/topic/194789-update-multiple-rows-with-same-variable/
Share on other sites

The problem is that you are giving the input fields the same name (and it is not an array). Therefore only one value is passed. Just change the field name to be an array using square brakets, like so:

 
<input type="hidden" readonly="readonly" name="goal_id[]" id="goal_id" value="<?php echo $row1['goal_id'];?>" size="12"/>

 

Then in your receiving page, you can create one UPDATE query using the IN control:

$idList = implode(',', $_GET['goal_id']);  //Assumes numeric field
$query = "UPDATE table SET filed='value' WHERE goal_id IN ({$idList})";

 

Also, you need to give every element a unique ID, you cannot use the same ID twice.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.