Page 1 of 1

How to update data using checkbox??

Posted: Sun Oct 25, 2009 11:02 pm
by code_newbie
I am a php newbie. Currently i am working on a project and i get stuck with updating data using checkbox.

The system that i build could update all data correctly if i tick 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 :)

The data is pass from manage_rma.php to update.php

Code: Select all

 
//manage_rma.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];
                        $m_phone=$row[10];
                        $email = $row[11];
                        
                        $product = $row[13];
                                            
                        $arrival_date=$row[16];
                        $fault_desc = $row[28];
                        $status=$row[29];
                        $remark=$row[30];
                        $tracking_num=$row[31];
 
 
 
echo '<tr bgcolor="#e9e6ed"><td width=5><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=left><?php echo $email;?> / <?php echo $m_phone; ?></td><td align=center><?php echo $product;?></td>
<td align=center><?php echo $arrival_date;?></td>
<td><input type=text name="remark[]" value="<?php echo $remark; ?>"></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: Select all

 
//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'];
 
$status_value =$_POST['status'];
$remark =$_POST['remark'];
$track =$_POST['trackingnumber'];
 
for($i=0; $i<count($box); $i++){
$up_id = $box[$i];
$remarks =$remark[$i];
$tracks = $track[$i];
 
$sql = "UPDATE warranty SET remark='".$remarks."', tracking_num='".$tracks."' WHERE id='".$up_id."'";
$result = mysql_query($sql) or die ("Error running MySQL query");
echo $remarks , $tracks;    
 
}
 
 
 
include('manage_rma.php');
 
 
?>