Results 1 to 2 of 2

Thread: Update data using checkbox ( php and mysql), How to update data using checkbox

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Location
    Malaysia
    Posts
    86

    Update data using checkbox ( php and mysql), How to update data using checkbox

    Hi there,

    I am a php beginner. Currently i am working on some testing php and i get stuck with updating data using checkbox.

    The system that i build could update all data correctly if i check all the checkbox and then click submit button.

    But there is a problem when i randomly select a row from table to update data. The data is updated according to the first row of data and not the data that i keyed in(i.e, the data is updated same with the value of first row in the table). Below is the codes. Hope someone could enlighten me

    Code:
    //manage_form.php
    <?php
    $sql = "SELECT * FROM warranty";
    $sresult=mysql_query($sql) or die(mysql_error());
    if(mysql_numrows($sresult))
    {
    while($row = mysql_fetch_row($sresult))
    {
    $id = $row[0];
    
    $orderid=$row[1];
    
    $cust_name=$row[2];
    
    $email = $row[11];
    
    $product = $row[13];
    
    $arrival_date=$row[16];
    $fault_desc = $row[28];
    $status=$row[29];
    
    $tracking_num=$row[30];
    
    echo '<tr bgcolor="#e9e6ed"><td><input type=checkbox name=box[] id=box[] value="'.$id.'"</td>';
    echo '<td align=center ><a href="index_.php?orderid='.$orderid.'&boxaction=display_form" target="_blank">'.$orderid.'</td>'; ?>
    <td align=center><?php echo $cust_name;?></td><td align=center><?php echo $email; ?></td><td align=center><?php echo $product; ?></td>
    <td width=150><?php echo $fault_desc; ?></td><td align=center><?php echo $arrival_date; ?></td>
    <td align="center"><select name=status[]><option value=pending <?php if($status=="pending") echo "selected";?>>Pending</option><option value=processing <?php if($status=="processing") echo "selected";?>>Processing</option><option value=sent <?php if($status=="sent") echo"selected";?>>Sent</option></select></td><td align=center><textarea name=trackingnumber[]><?php echo $tracking_num; ?></textarea>
    </td></tr>
    <?php
    }
    echo '<tr><td><input type="submit" name="submit" Value="Update"></td><td><input type="submit" name="submit" Value="Delete" onclick="ConfirmChoice();return false;"></td></tr>';
    }
    else{
    print("<tr><td><B>No record found<B></td><tr>");
    }
    
    ?>
    Code:
    //update.php
    
    <?php
    include('auth.php');
    include('database_access_param.php');
    
    
    $con = mysql_connect($hostname,$dbuser,$dbpassword)or die(mysql_error());
    $db = mysql_select_db($dbname,$con)or die(mysql_error());
    
    $box=$_POST['box'];
    $track=$_POST['trackingnumber'];
    $status_value=$_POST['status'];
    
    
    for($i=0; $i<count($box); $i++){
    $up_id = $box[$i];
    
    $tracks = $track[$i];
    $statuss = $status_value[$i];
    $sql = "UPDATE warranty SET status='".$statuss."', tracking_num='".$tracks."'WHERE id='".$up_id."'";
    $result = mysql_query($sql) or die ("Error running MySQL query");
    
    }
    include('manage_form.php');
    ?>
    I have attached a screen shot of the project. In the picture, there is 2 row of data. It is work fine if i want to update the first row data or update 2 rows of data at the same the time.

    The problem is, when i wanna update the second row, the data will not updated as the data that i keyed in the textarea(tracking number column) instead it will updated with the data as the first row. I have tested will 5 row of data before this, all the subsequent rows data will be replaced with the value same as the first row.

    I guess there is a problem with looping problem, but I do not know how to solve it.

    Thanks
    Attached Images Attached Images  
    You can see me as i can see you.. Peace

Posting Permissions

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



Click Here to Expand Forum to Full Width