Update query

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

  1. #1
    Hello all,

    What is wrong with this update query? Can anybody help me? i do not get any error but it is not updating anything

    Thanks


    
    
    <?
    include_once ("config/connect.php");
    
    $ref=$_GET['ref'];
    
    if (isset($_POST['ok'])) 
    {
    
         $item_name = $_POST['item_name'][$i];
         $worker = $_POST['worker'][$i];	 
         $item_id = $_POST['item_id'][$i];
    
         for($i=0; $i < $count; $i++){
    
         $query5 = mysql_query("UPDATE item SET item_name = '".$item_name."', worker_id = '".$worker."' WHERE item_id = '".$item_id."' ") or die(mysql_error());
         }            
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    <body>
    
    <form action="<?=$PHP_SELF;?>" method="post">
    <?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="300">
    <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>
    <?php
    for ($i=0; $i<$row1['item_amount']; $i++) {
    ?>
    Choice a worker for this job
    <select name="">
    <option>select</option>
    <?php
      
      for ($s = 1; $s<=$row1['item_amount']; $s++) {
    		echo '<option>'.$s.'</option>';
    		  
      }
    
    ?>
    
    </select>
    <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 } ?> 
        <br />
       <input type="submit" name="ok" value="Submit" onclick="return confirmPost()" />
       </form>
    </body>
    </html>
    
    
    
    PHP:
     
    baris22, Oct 19, 2009 IP
  2. orionoreo

    orionoreo Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't see $count anywhere prior to the update
     
    orionoreo, Oct 19, 2009 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thank you again. I can kind of update now but there is a problem.

    It only updates 1 row. If there are 2 rows which has got item_amount value "2" it does not update.

    I think it is because of part of my query "GROUP BY item_amount"

    Do you have any suggestion for this

    
    
    <?
    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());
         }            
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    <body>
    
    <form action="<?=$PHP_SELF;?>" method="post">
    <?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="300">
    <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>
    <?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 } ?> 
        <br />
       <input type="submit" name="ok" value="Submit" onclick="return confirmPost()" />
       </form>
    </body>
    </html>
    
    
    
    PHP:
     
    baris22, Oct 19, 2009 IP
  4. orionoreo

    orionoreo Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you noticed that you have set the for loop after the variables... you have to do everything before right here

    
         $item_name = $_POST['item_name'][$i];
         $worker = $_POST['worker'][$i];     
         $item_id = $_POST['item_id'][$i];
    
         for($i=0; $i < $count; $i++){
    
     
         $query5 = mysql_query("UPDATE item SET item_name = '".$item_name."', worker_id = '".$worker."' WHERE item_id = '".$item_id."' ") or die(mysql_error());
    
         }  
    
    PHP:
     
    orionoreo, Oct 20, 2009 IP
  5. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    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
  6. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Thank you for your help. One last question

    
    
     <?php
    $query99 = "SELECT * FROM item WHERE order_reference_number='$ref'";
    $portfolio = mysql_query($query99);
    while($row109 = mysql_fetch_array($portfolio)) {
    ?>
    <?=$row109['item_name'];?>
    
    
    PHP:
    this is the code. How can I make an if statement that if <?=$row109['item_amount'];?> is bigger than 1 display <?=$row109['item_name'];?> only once.

    Thank you very much
     
    baris22, Oct 20, 2009 IP
  7. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #7
    hello,

    This is the last code.

    
    
    <?
    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());
         
    	 }     
    	     
    }
    
    ?>
    
    <form action="<?=$PHP_SELF;?>" method="post">
    <table width="600" border="0" cellpadding="4" cellspacing="0" bordercolor="#CCCCCC">
    
      <tr>
        <td height="24" bgcolor="#999999"><strong>Item amount</strong></td>
        <td height="24" bgcolor="#999999"><strong>Item name</strong></td>
        <td height="24" bgcolor="#999999"><strong>choice a worker</strong></td>
        <td height="24" bgcolor="#999999"><strong>chosen worker</strong></td>
      </tr>
      <tr>
    <?php
    $query99 = "SELECT * FROM item WHERE order_reference_number='$ref'";
    $portfolio = mysql_query($query99);
    $i = 0;
    while($row109 = mysql_fetch_array($portfolio)) {
    ?>
        
        <td height="32"> &nbsp;&nbsp; <?=$row109['item_amount'];?></td>
        <td height="32"> &nbsp;&nbsp; <?=$row109['item_name'];?>
    	<input name="item_id[]" type="hidden" value="<?=$row109['item_id'];?>" /></td>
        
        
        <td> &nbsp;&nbsp; 
        <select name="worker[]" id="">
        <option value="<?=$row109['worker_id'];?>"><?=$row109['worker_id'];?></option>
        <option value="-">-</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> &nbsp;&nbsp; <a href="g">
          <strong><?=$row109['worker_id'];?></strong>
           </a></td>
      </tr><?php
    }
    ?>
    </table>
    
    <input type="submit" name="ok" value="Submit" onclick="return confirmPost()" />
    </form>
    
    
    
    PHP:
    This is the database:

    [​IMG]

    This is the output of my code:

    [​IMG]

    I want to display item_amount and item_name only once if the item_amount is bigger than 1.

    [​IMG]

    Can somebody help me please.
    Thanks
     
    baris22, Oct 21, 2009 IP