dimanche 11 juillet 2021

PHP checkbox updates wrong row ID

I have a form that has multiple rows all with the same number of checkboxes, each row has 5 checkboxes and a form can have any number of rows. Each row in the form is a different row in the database.

When starting with a blank form I am able to enter each new row correctly, however when editing an existing row I have a problem. Instead of updating the row where the box is checked it updates the first entry in the database, if that is already checked it updates the next one.

This is my code for the form (just one row though)

<div class="form-group row">
        <label for="inputFirstName" class="col-form-label col-sm-3 text-left">Unit 
Name</label>
        <label for="inputFirstName" class="col-form-label col-sm-1 text-left">Model 
Count</label>
        <label for="inputFirstName" class="col-form-label col-sm-1 text-left">Bought?</label>   
        <label for="inputFirstName" class="col-form-label col-sm-1 text-left">Built?</label>    
        <label for="inputFirstName" class="col-form-label col-sm-1 text-left">Primed?</label>
        <label for="inputFirstName" class="col-form-label col-sm-1 text-left">Painted?</label>
        <label for="inputFirstName" class="col-form-label col-sm-1 text-left">Based?</label>
        <label for="inputFirstName" class="col-form-label col-sm-1 text-left">Cost <small> 
(pts)</small></label>
        <label for="inputFirstName" class="col-form-label col-sm-1 text-left">Cost <small> 
(£/$)</small></label>
        </div>
                        
    <div class="form-group row">        
    <input style="width:50px;" class="form-control" type="hidden" name="uid[]" value="3">
<div class="col-sm-3">
<input class="form-control" type="text" name="name[]" value="Ehrwig Arellano" required>
</div>
<div class="col-sm-1">
<input class="form-control" type="text" name="count[]" value="6">
</div>
<div class="col-sm-1">
<input name="bought[]" type="checkbox" value="1" class="form-control" />    </div>  
<div class="col-sm-1">
    <input name="built[]" type="checkbox" value="1" class="form-control" /> </div>  
<div class="col-sm-1">
    <input name="primed[]" type="checkbox" value="1" class="form-control" />    </div>          
<div class="col-sm-1">
    <input name="painted[]" type="checkbox" value="1" class="form-control" />   </div>  
<div class="col-sm-1">
    <input name="based[]" type="checkbox" value="1" class="form-control" /> </div>  
<div class="col-sm-1">
<input class="form-control" type="text" name="points[]" value="">
</div>
<div class="col-sm-1">
<input class="form-control" type="text" name="price[]" value="">
</div>      
</div>

This is the code for mysql insert.

foreach ($_POST['name'] as $index => $unitnameid) {
    if($unitnameid != '')
{
    $unitname = tep_db_prepare_input($unitnameid); 
    $uid = tep_db_prepare_input($_POST['uid'][$index]);
    $bought = isset($_POST['bought'][$index]); 
    $built = isset($_POST['built'][$index]); 
    $primed = isset($_POST['primed'][$index]); 
    $painted = isset($_POST['painted'][$index]); 
    $based = isset($_POST['based'][$index]);
    $count = tep_db_prepare_input($_POST['count'][$index]);
    $points = tep_db_prepare_input($_POST['points'][$index]);
    $price = tep_db_prepare_input($_POST['price'][$index]);
    $project_id = $header_id;   
               if($uid != ''){
          tep_db_query("update project_line SET unit_name = '".tep_db_input($unitname)."', 
bought = '".tep_db_input($bought)."', built = '".tep_db_input($built)."', primed = 
'".tep_db_input($primed)."', painted = '".tep_db_input($painted)."', based = 
'".tep_db_input($based)."', count = '".tep_db_input($count)."', points = 
'".tep_db_input($points)."', price = '".tep_db_input($price)."' where project_id = '".$pid."' 
AND line_id = '".$uid."'");
        } else { 
    tep_db_query("insert into project_line values ('NULL', '".$project_id."', 
'".tep_db_input($unitname)."', '".tep_db_input($bought)."', '".tep_db_input($built)."', 
'".tep_db_input($primed)."', '".tep_db_input($painted)."', '".tep_db_input($based)."', 
'".tep_db_input($count)."', '".tep_db_input($points)."', '".tep_db_input($price)."')")
       
 or die(mysql_error());
        }
    }
    }

Any help is greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire