1

I have a table(myTable) with 3 fields:

ID (int), posistion(int), products_id(int)

I have an form where we can change the order, and remove items from/in this list. After the user is done editing the list he submits the form.

I want to empty out the table before inserting the new data.

so i call $this->db->empty_table("myTable");

After that i insert the new data like this:

$this->db->insert_batch("jcarousel",$insert);

The problem is: somehow the empty_table() is called after the insert query, because my table always is empty. To be clear: My insert query is working fine when i comment out //$this->db->empty_table("myTable");

i have tried a couple of things, so my code i a bit messed up. Right now it looks like this:

public function change_carousel_order($value='')
{
    $this->output->enable_profiler(TRUE);
    $empty = $this->empty_table();
    //$empty = TRUE;
    $form = $this->input->post();
    //print_r($form['order']);
    $insert = array();
    foreach ($form['order'] as $key => $value) {
        //echo "value: ".$value;
        $insert[$key]['product_id'] = $value;
        $insert[$key]['posistion'] = $key;

    }
    echo $this->db->last_query();
    if($empty==TRUE)
    {
        echo "jaaa";
        echo $this->insert_change_carausel_order($insert);
    }
    else{
        echo "neee!";
    }
    echo $this->db->last_query();
    //redirect("welcome/jcarousel");
}
public function empty_table($value='')
{
    return $this->db->empty_table("jcarousel");
}
public function insert_change_carausel_order($insert=array())
{
    return $this->db->insert_batch("jcarousel",$insert);
}

Anyone who see what i am doing wrong? Why is'nt codeigniter inserting any data after empty_table() is called? Any help would be great full appreciated.

$this->output->enable_profiler(TRUE); tells us that there are 2 queries; the delete query runs indeed AFTER the insert query

0.0003       DELETE FROM `jcarousel` 
0.0002       INSERT INTO `jcarousel` (`posistion`, `product_id`) VALUES (0,'755'), (1,'835'), (2,'838') 

and another result same setup:

0.0006       DELETE FROM `jcarousel` 
0.0002       INSERT INTO `jcarousel` (`posistion`, `product_id`) VALUES (0,'755'), (1,'835'), (2,'838') 
13
  • what you get if var_dump($empty) ? Commented Nov 26, 2012 at 8:49
  • var_dump($empty) returns: bool(true) Commented Nov 26, 2012 at 8:51
  • what is you get echo $this->db->last_query(); after $this->insert_change_carausel_order($insert); Commented Nov 26, 2012 at 8:54
  • INSERT INTO jcarousel (posistion, product_id) VALUES (0,'755'), (1,'835'), (2,'838') Commented Nov 26, 2012 at 8:55
  • when and how you are calling change_carousel_order function. can you paste that code as well Commented Nov 26, 2012 at 9:18

2 Answers 2

0

I think you have spelling mistake issue.

As you stated

ID (int), position(int), products_id(int)

But in array you used as below

//wrong
$insert[$key]['product_id'] = $value;

//correct
$insert[$key]['products_id'] = $value;

//wrong
$insert[$key]['posistion'] = $key;

//correct
$insert[$key]['position'] = $key;
Sign up to request clarification or add additional context in comments.

2 Comments

true i have spelling mistake, but i made the spelling mistake everywhere (except for the question). plus my insert query works fine when i command out the "$this->empty_table();" part
no, sorry, that was not the problem i am facing check my edited Question
0

The problem was chrome's firebug plugin. this causes the page to be loaded twice, there for the second time it loads it lost all its post data.

Hope i can help someone with this (beter late then never) awnser

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.