2

I have a table menu w/c contains of(recipe_id,ingredient_id,category_id).I was trying to update my ingredients yet it only update 1 ingredient_id. Like this

menu

Here is my code:

CONTROLLER:

public function save_edit_recipe()
{
    foreach ($this->input->post('ingredients') as $key => $value) {
        $menuData[] = array(
            'recipe_id'     => intval($this->input->post('recipe_id')),
            'ingredient_id' => intval($value),
            'category_id'   => intval($this->input->post('recipe_category'))
        );
    }
    // var_dump($menuData); die();
    $this->products_model->updatemenu($menuData);
}

menudata is:

Array (
    [0] => Array (
        [recipe_id] => 2
        [ingredient_id] => 1
        [category_id] => 3
    )
    [1] => Array (
        [recipe_id] => 2
        [ingredient_id] => 2
        [category_id] => 3
    )
)

MODEL:

public function updatemenu($data)
{
    foreach ($data as $row => $value) {
        $this->db->where('ingredient_id', $data['ingredient_id']);
        $query = $this->db->update('menu', $value);
    }
    return $result;
}
0

2 Answers 2

3

Remove all data in controller and add in to model.

In Controller

$this->products_model->updatemenu();

In Model

public function updatemenu()
{
    foreach($this->input->post('ingredients') as $key => $value)
    {
        $menuData = array(
            'recipe_id'         => intval($this->input->post('recipe_id')),
            'ingredient_id'     => intval($value),
            'category_id'       => intval($this->input->post('recipe_category'))
        );
        $this->db->where('ingredient_id', $value);
        $this->db->update('menu', $menuData);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Check with this. Im not 100% sure. Before check change your data field as it was early too
@shikira I flagged your comment. You should delete that type of data set publicly.
I think no need to add ingredient_id in update array bro. Or any other column will use for where clause
This answer seemingly fixes nothing. It is not the responsibility of the model to access submission data. Doing so makes unit testing more tedious. It is better practice to pass all required data into the model method. The model should be querying the database and virtually nothing else.
0

Your screenshot seems to indicate that:

  • ingredient_id is not a unique column in the table, and
  • ingredient_id 1 occurs twice, and
  • ingredient_id 2 does not exist.

Therefore, trying to UPDATE ingredient_id 1 will affect 2 records and ingredient_id 2 will affect no records. There is evidence that you unintentionally corrupted your database ingredient_id values during earlier development.

Regardless, if you want a process that can insert or update a record, you should make ingredient_id a unique primary key and call CodeIgniter's replace() method.

public function updatemenu(array $rows): int
{
    $affectedRows = 0;
    foreach ($rows as $row) {
        if ($this->db->replace('menu', $row, ['ingredient_id' => $row['ingredient_id']])) {
            $affectedRows += $this->db->affected_rows();
        }
    }
    return $affectedRows;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.