Welcome Guest, Not a member yet? Register   Sign In
Active Records inserting a duplicate row?
#1

[eluser]Haven Ethic[/eluser]
I'm on CI 2.10 with PHP 5.3 and MySQL. I'm having a strange issue where an insert seems to be running twice--totally baffled.

Here is the relevant code:

[Notice the comment in the controller, in the topping_cats_view function.]

View:
Code:
<?php
  if (isset($menu_items)){
   foreach($menu_items->result() as $item){
    $enabled = $item->enabled;
    if ($enabled == FALSE) continue;
    $name = $item->item_name;
    $price = $item->price;
    $top_price = $item->topping_price;
    $show_top_price = $item->show_top_price;
    $combo_top_prices = $item->combo_top_prices;    
    $desc = $item->description;
    $table = $item->toppings_table;
        
    echo '<div class="menu_item">';
    echo '<a href="'.site_url('order/add_item').'/'.$name.'/'.$price.'/'.$table.'/'.$combo_top_prices.'/'.$top_price.'">'.$name.'</a>';
    if ($show_top_price == TRUE && $combo_top_prices == FALSE){
     echo '<div><p>'.$desc.'    $'.$price.'     (Toppings $'.$top_price.')</p></div>';
    }else {
     echo '<div><p>'.$desc.'    $'.$price.'</p></div>';
    }
    
    echo '</div>';
   }
  }
?&gt;

Controller:
Code:
function add_item($item_name, $item_price, $toppings_table, $combo_top_prices, $top_price) {
  $item_name = urldecode($item_name);
  $order_id = $this->session->userdata('order_id');
      
  $item_id = $this->order_model->add_item($order_id, $item_name, $item_price, $combo_top_prices, $top_price);
  
  if ($item_id  < 1){
   $data = array (
      'title'=>'Order Problem',
      'content'=>'order/order_problem_view',
      'view_stuff'=>array('problem'=>'Well this is embarrassing!  <br/>For some reason there was a problem adding '.$item_name.'  Please try again.')
      );
   $this->load->view('includes/template', $data);
   return;
  }
  
   /* determine if we need toppings and act */
   if ($toppings_table == 'none'){
    $this->session->unset_userdata('current_item_id');
    $this->session->unset_userdata('current_item_name');
    $this->session->unset_userdata('current_item_combo_top_prices');
    $this->session->unset_userdata('current_item_top_price');
    
    $results = $this->order_model->get_menu_cats($toppings_table);
    $data = array (
      'title'=>'Order',
      'content'=>'order/order_main_view',
      'view_stuff'=>array('menu_categories'=>$results)
     );
    $this->load->view('includes/template', $data);
    return;
   }else {
    
    $this->session->set_userdata('current_item_id', $item_id);
    $this->session->set_userdata('current_item_name', $item_name);
    $this->session->set_userdata('current_item_combo_top_prices',$combo_top_prices);
    $this->session->set_userdata('current_item_top_price', $top_price);
    
    $this->toppings_cat_view($toppings_table);
   }
  
  
}

function toppings_cat_view($toppings_table){
  
  $results = $this->order_model->get_topping_cats($toppings_table);
  echo $this->db->last_query();
  $this->session->unset_userdata('current_topping_name');
  $this->session->unset_userdata('current_topping_price');
  $data = array (
      'title'=>'Order',
      'content'=>'order/order_toppings_cat_view',
      'view_stuff'=>array('topping_categories'=>$results, 'toppings_table'=>$toppings_table)
     );
   $this->load->view('includes/template', $data);
  
/* die('check it!');   <---if I die here it doesn't insert a duplicate record!!! */

/* If I go into the target view and die after all php is executed there is still no "duplicate" row. The last query reported just looks like session management. */
}

Model:
Code:
function add_item($order_id, $item_name, $price, $combo_top_prices, $top_price){
  //echo $price;
  $data = array (
   'order_id'=>$order_id,
   'name'=>$item_name,
   'price'=>$price,
   'combo_top_prices'=>$combo_top_prices,
   'top_price'=>$top_price
  );
  
  $this->db->insert('order_items', $data);
  
  $item_id = $this->db->insert_id();
  
  return $item_id;
  
}

function get_topping_cats($toppings_table){
  
  $this->db->select('category');
  $this->db->distinct();
  $results = $this->db->get($toppings_table);
  
  return $results;
}

Database entry:
[The one with ID# 136 is not supposed to be there.]
Code:
ID    Order_id  Item_name
136  129  Small Hot Dog      0     0
135  129  Small Hot Dog      5.99  0

WTF!?
#2

[eluser]Haven Ethic[/eluser]
Saw another post like this on Stackoverflow. It was unanswered.
#3

[eluser]Haven Ethic[/eluser]
I don't think this has anything to do with Codeigniter or PHP. [EDIT: I do not know where this problem stems from.]

2011: http://www.webmasterworld.com/php/4279219.htm

2005: http://www.webmasterworld.com/forum88/9635.htm

~2012: http://stackoverflow.com/questions/60488...-statement

There is something bugged up in Apache that is very hard to reproduce. Maybe it has something to do with shared accounts. I can't restart my Apache server unfortunately.
#4

[eluser]Haven Ethic[/eluser]
I managed to convince my hosting company to reset the shared Apache server. No Dice. I also enabled PHP 5.3 as well. Still having issues. If I kill the final PHP script before it falls through to HTML it does not record a duplicate record.
#5

[eluser]Haven Ethic[/eluser]
In the add_item() function I reduced the number of parameters to just $item_name and then got the rest of the information from the database to continue the function. Everything was being passed in as expected from the view--I verified this numerous times of course--so everything was checking out. I was originally using GET (Query Strings) to pass parameters into the add_item function and recently converted to use the URL segment method available in CI. This caused some type of anomalous data loss behind the scenes. It reminded me of what happens when someone uses a pass by reference instead of pass by value, leading to "buggy" behavior.

For some reason this change eliminated all the issues I was having--there was also session data (one variable!) that was disappearing when the target view loaded.

Needless to say, I do not fully trust the URL segment feature now. Also, I was using query strings without enabling them in the config.php file and they worked without any problems. (??)

I have switched back to using query strings and $this->input->get() to pass parameters. The URL segments do not work and no I have NOT enabled query strings in the config.php file. Everything is working great now.

This may not be a CI issue but an Apache/PHP problem.

Linux x86 CentOS 5.5
Apache 2.2.21
PHP 5.3.10
Perl 5.8.8
MySQL 5.1.52





Theme © iAndrew 2016 - Forum software by © MyBB