1

I get the following error:

A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: models/Bulk_Recipeis_model.php

Line Number: 32

This my code in the model:

public function insert_order($user, $recipies, $quantity, $date, $time, $totalbill)
{
    $this->db->trans_start();
    foreach($quantity as $key => $user_quantity) {
        $this->db->query("UPDATE bulk_delivery SET max_quantity = max_quantity - '$user_quantity'");
        $this->db->query("INSERT INTO bulk_delivery_order (id, user_quantity, date,time,total_bill,bulk_delivery_id) VALUES ('$user', '$user_quantity', '$date', '$time', '$totalbill', '$recipies')");
    }
    $this->db->trans_complete();
}

My code in the controller:

public function get_insert_order(){
    $user = $this->userId;
    $recipies = $this->input->post('bulk_delivery');
    $quantity = $this->input->post('quantity');
    $date = $this->input->post('date');
    $time = $this->input->post('time');
    $totalbill = $this->input->post('bill');
    $this->bulk_recipe->insert_order($user, $recipies, $quantity, $date, $time, $totalbill);
    redirect('BulkRecipe_Controller');
}

It's in my view where I used array data:

<input type="hidden" name="bulk_delivery[]" value="<?php echo $row->bulk_delivery_id;?>" >
<input type="text" name="quantity[]" step="1" class="container" value="" onfocus="this.value = '';" onblur=";" style="width: 60px">

Please tell me how to resolve this error.

Community
  • 1
  • 1
  • 1
    It's usually helpful to know, which of the lines you pasted is the line 32. – jedrzej.kurylo Aug 13 '15 at 15:00
  • I believe `$quantity` will have numbers. Use `$user_quantity` instead of `'$user_quantity'`. –  Aug 13 '15 at 15:03
  • in this line error is pointed VALUES ('$user','$user_quantity','$date','$time','$totalbill','$recipies')"); – falak hamid Aug 13 '15 at 15:03
  • @codeSun i also applied your thought replace '$user_quantity' with $user_quantity but till not resolve – falak hamid Aug 13 '15 at 15:06
  • Did you change here `UPDATE bulk_delivery SET max_quantity = max_quantity - $user_quantity`? –  Aug 13 '15 at 15:20
  • why are you coming up with multiple queries? You can just build one query with multiple values. – CodeGodie Aug 13 '15 at 15:27
  • its may be due to quantity with null value. Please check $quantity before calling model – Vinie Aug 13 '15 at 15:31
  • one of your values is in array format, but they need to be string. Can you `var_dump` the output of all your variables passed and share that with us? – CodeGodie Aug 13 '15 at 15:36
  • Your code you posted has a SQL injection vulnerability. See: http://stackoverflow.com/q/60174/17287. The solution is to use parameterized queries (`$this->db->query("UPDATE bulk_delivery SET max_quantity = max_quantity - ?", array($user_quantity));`) – mcrumley Aug 13 '15 at 19:06

1 Answers1

0

change your model to this --

public function insert_order($user, $recipies, $quantity, $date, $time, $totalbill)
        {
            $this->db->trans_start();
            foreach($quantity as $key => $user_quantity) {
                $final_quantity = $user_quantity['key'];
                $this->db->query("UPDATE bulk_delivery SET max_quantity = max_quantity - '$final_quantity'");
                $this->db->query("INSERT INTO bulk_delivery_order (id, user_quantity, date,time,total_bill,bulk_delivery_id) VALUES ('$user', '$final_quantity', '$date', '$time', '$totalbill', '$recipies')");
            }
            $this->db->trans_complete();
        }
Sorav Garg
  • 1,116
  • 1
  • 9
  • 26