How to update multiple colunms in mysql php pdo using arrays

When i click update btn its only updating the colunms with single colunm when i select a form with multiple colunms its only updating the last entry the last id

if i select the colunm with more data in it its only updating the last id in the database not everythings but where there is a single colunm its updating both tables with no problem

this is the form for updating and its showing all the ids for each invoice

<tbody class="detail">
<?php 
$count= 0; 
foreach ($getdata as $invoices):
    $count++;  
?>
<tr>
    <div class="inputfield">
        <td class="no">1</td>
        <td >
            <textarea name="productname[]" class="productname" id=""  cols="35"  rows="2" >
                <?php echo $invoices->productname; ?>
            </textarea>
        </td>
        <td >
            <input id="quantit" name="quantity[]" class="quantity" 
                    value="<?php echo $invoices->quantity; ?>" 
                    type="text"  onchange="Calc(this);">
        </td>
        <td >
            <input id="prices" name="price[]" class="price" 
                    value="<?php echo $invoices->price; ?>" 
                    type="text" onchange="Calc(this);">
        </td>
        <td >
            <input  name="amount[]" class="amount" 
                    value="<?php echo $invoices->amount; ?>"    
                    type="text"  >
        </td>
        <input type="hidden" name="item_id" value="<?php echo $invoices->item_id; ?>">
    </div>
</tr>

<?php 
endforeach; 
?>

</tbody>

this is where the action is taking place and when i clich update only one id here is showing and not all the ids

$invoices_id = $_GET['invoices_id'];
$invoice_fk = $_GET['invoices_id'];

if (isset($_POST['edit_invoice'])) {
    //Creatr Data array
    $data = array();
    $data['invoices_id']    = $_POST['invoices_id'];
    $data['invoice_fk']     = $_POST['invoice_fk'];
    $data['productname']    = $_POST['productname'];
    $data['quantity']       = $_POST['quantity'];
    $data['price']          = $_POST['price'];
    $data['amount']         = $_POST['amount'];
    $data['sub_total']      = $_POST['sub_total'];
    $data['total_tax']      = $_POST['total_tax'];
    $data['total']          = $_POST['total'];
    $data['customer_fk']    = $_POST['customer_fk'];
    $data['item_id']        = $_POST['item_id'];
    $data['companies_fk']   = getCompany()['companies_fk'];
    $data['last_activity']  = date("Y-m-d H:i:s");

    //die(print_r($data['item_id'] = $_POST['item_id']));

    $field_array = array('productname','quantity', 'price');
    if ($validate->isRequired($field_array)) {
        //Update  Invoice
        if ($invoice->updateInvoice($data)){

            redirect('updateinovice.php?invoices_id='.$invoices_id, 'Invoice Edited', 'success');
        } else {
            redirect('updateinovice.php?invoices_id='.$invoices_id, 'Invoice not edited something went   wrong', 'error');
        }
    } else {
        redirect('updateinovice.php?invoices_id='.$invoices_id, 'All fields are required',  'error');
    }
}

this is where the model is and updating is taking place have tried showing the id its only showing last id in the database for multiple data

public function updateInvoice($data){
    try {
        $this->db->query('UPDATE `tbl_invoice_total` 
                            SET customer_fk = :customer_fk, 
                                companies_fk = :companies_fk, 
                                sub_total = :sub_total ,
                                total_tax = :total_tax,
                                total = :total, 
                                last_activity =:last_activity 
                            WHERE invoices_id = :invoices_id ');

        $this->db->bind(':customer_fk', $data['customer_fk']);
        $this->db->bind(':companies_fk', $data['companies_fk']);
        $this->db->bind(':sub_total', $data['sub_total']);
        $this->db->bind(':total_tax', $data['total_tax']);
        $this->db->bind(':total', $data['total']);
        $this->db->bind(':last_activity', $data['last_activity']);
        $this->db->bind(':invoices_id', $data['invoices_id']);
        $this->db->execute();

        foreach ($data['productname'] as $key => $value) {
            $this->db->query('UPDATE tbl_invoice_items 
                                SET invoice_fk =:invoice_fk, 
                                    productname = :productname ,
                                    quantity = :quantity,
                                    price = :price, 
                                    amount = :amount, 
                                    last_activity =:last_activity 
                                WHERE item_id = :item_id');
            $this->db->bind(':invoice_fk', $data['invoice_fk']);
            $this->db->bind(':productname', $data['productname'][$key]);
            $this->db->bind(':quantity', $data['quantity'][$key]);
            $this->db->bind(':price', $data['price'][$key]);
            $this->db->bind(':amount', $data['amount'][$key]);
            $this->db->bind(':last_activity', $data['last_activity']);
            $this->db->bind(':item_id', $data['item_id']);
            $this->db->execute();
        }
        //die(print_r($data));
        return true;
    } catch (Throwable $e) {
        echo '<div class="error">'.get_class($e).' on line '.$e->getLine().' of '. $e->getFile().': '.$e->getMessage().'</div>';
    }
}

I searched on google could not find something similar to my problem

I tried using for each to echo id on the form it showed but when i use die to echo it its only showing the last id

I tried to search on youtube i could not find something similar to my challenge

what i want is when i click update it must update multiple colunms it must be able to update all the colunms not only updating a last colunm

  • 2

    All your other form fields are arrays, but your item_id is not – I suggest you change name="item_id" to name="item_id[]", resubmit your form and do a print_r($_POST); exit; just to check you’re receiving what you expect

    – 

  • Good code indentation and layout is for life, not just for Christmas and would help us read the code and more importantly it will help you debug your code Take a quick look at a coding standard for your own benefit. You may be asked to amend this code in a few weeks/months and you will be glad you took the time in the end.

    – 

  • 1

    fyi, <div> inside <tr> is not allowed and <input> inside <tr> is not allowed. You also have multiple elements with id="quantit" and id="prices" – which is invalid, ids must be unique

    – 




  • Sure let me just do that now

    – 

  • It has worked i also added a $this->db->bind(‘:item_id’, $data[‘item_id’][$key]); and it worked wow thanks how do i make the answer

    – 

Leave a Comment