1

I have a project on inventory system. a user can pay for an order step by step if they can't offer the whole payment at once. for eg. if a user wants to buy Laptop, and the price is $1000, they can pay 300 on their first payment, $500 in second round, and $200 in third round. I tried it by inserting the value as an array in mysql database, i updated the payment after i write some code. the code is.

i am using codeigniter

VIEW

MODEL public function getPreviousPayment($id) {

    $sql = "SELECT paymentA FROM orders WHERE id = ?";
    $query = $this->db->query($sql, array($id));
    return $query->result_array();

}

CONTROLLER

public function payment($id) {
    $paymentA[] = $this->model_orders->getPreviousPayment($id);

    $paymentN = $this->input->post('paymentNext');
    $paymentNArray = [$paymentN];

    $newArr = array_merge($paymentA ,$paymentNArray);

    $data = array(  
        'paymentA' =>  json_encode($newArr),
    );
}

what i get is ... look at the screen shot below my fetched table when nothing is pad

first payment

second payment

robel
  • 109
  • 3
  • 12
  • 3
    The correct thing to do would be normalizing your schema, rather than storing all the values in a single column (see [here](https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad) to find out why that's generally a bad idea). Then it would be much simpler to insert/update individual payment values. – El_Vanja Apr 21 '21 at 09:09

1 Answers1

0

You Have to normalize your schema by including an intermediate table to store each payment, In Another way:Let say if you don't need to know each payment values (You only need to know how much the user totally paid then You can simply add each payment value to existing column value. I hope You Understood.

JEJ
  • 814
  • 5
  • 21