Updating multiple rows.

Discussion in 'PHP' started by baris22, Oct 20, 2009.

  1. #1
    Hello all,

    I need some help. This is my database:

    [​IMG]

    This is the output of my code:

    [​IMG]

    When I submit the form it only updates the first row. It does not update the second one.

    [​IMG]

    Can you please help me.
    Thanks

    
    
    <?
    include_once ("config/connect.php");
    $ref=$_GET['ref'];
    
    if (isset($_POST['ok'])) 
    {
         $item_name = $_POST['item_name'];
         $worker = $_POST['worker'];	 
         $item_id = $_POST['item_id'];
    
         for ($i=0;$i<count($_POST['item_id']);$i++) {
    
         $query5 = mysql_query("UPDATE item SET item_name = '".$item_name[$i]."', worker_id = '".$worker[$i]."' WHERE item_id = '".$item_id[$i]."' ") or die(mysql_error());
         }            
    }
    
    ?>
    
    
    <?php
    $query1 = "SELECT * FROM item WHERE order_reference_number='$ref' GROUP BY item_amount";
    $portfolio = mysql_query($query1);
    while($row1 = mysql_fetch_array($portfolio)) {
    ?>
    <table width="100%">
    <tr>
    <td valign="top" width="266">
    <?=$row1['item_amount'];?> of 
    <input name="item_name[]" type="text" value="<?php echo $row1['item_name'];?>" size="30" />
    <input name="item_id[]" type="hidden" value="<?=$row1['item_id'];?>" />
    </td>
    <td width="480">
    <?php
    for ($i=0; $i<$row1['item_amount']; $i++) {
    ?>
    Choice a worker for this job
    
    <select name="worker[]" id="">
    <option value="Select">Select</option>
    <?php  
    $query3 = "SELECT * FROM worker";
    $portfolio1 = mysql_query($query3);
    while($row3 = mysql_fetch_array($portfolio1)) {
    ?>    
    <option><?php echo $row3['worker_name'];?></option>
    <?php } ?>
    </select><br /><br />
    
    <?php
    }
    ?>
    </td>
    </tr>
    </table>
    <?php } ?> 
    
    
    PHP:
     
    baris22, Oct 20, 2009 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    mastermunj, Oct 20, 2009 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thanks for replies. I changed the codes and it is working now. 1 more question: How can choice a worker select box value can be remembered instead of displaying "Select" value

    [​IMG]

    
    
    <?
    include_once ("config/connect.php");
    $ref=$_GET['ref'];
    
         if (isset($_POST['ok'])) 
    {
    	 $worker = $_POST['worker'];	 
         $item_id = $_POST['item_id'];
    	 
    	
         for ($i=0;$i<=count($item_id);$i++) { 
    
         $query5 = mysql_query("UPDATE item SET worker_id = '".$worker[$i]."' WHERE item_id = '".$item_id[$i]."' ") or die(mysql_error());
         
    	 }     
    	     
    }
    
    ?>
    
    
    
    <table width="100%" border="1" cellpadding="4" cellspacing="4">
    <tr>
        <td><strong>Item</strong></td>
        <td><strong>choice a worker</strong></td>
        <td><strong>chosen worker</strong></td>
      </tr>
      <tr><?php
    $query99 = "SELECT * FROM item WHERE order_reference_number='$ref' ";
    $portfolio = mysql_query($query99);
    while($row109 = mysql_fetch_array($portfolio)) {
    ?>
        <td>
    	<?=$row109['item_name'];?>
        <input name="item_id[]" type="hidden" value="<?=$row109['item_id'];?>" />
        </td>
        <td>
        <select name="worker[]" id="">
    <option value="Select">Select</option>
    <?php  
    $query3 = "SELECT * FROM worker";
    $portfolio1 = mysql_query($query3);
    while($row3 = mysql_fetch_array($portfolio1)) {
    ?>    
    <option><?php echo $row3['worker_name'];?></option>
    <?php } ?>
    </select>
     </td>
        <td><?=$row109['worker_id'];?></td>
      </tr><?php
    }
    ?>
    </table>
    
    
    PHP:
     
    baris22, Oct 20, 2009 IP