0
votes

I'm trying to implement Php Codeigniter model function that can count the number of left children and the number of right children for binary tree but i got this message error :

Error Number: 1062

Duplicate entry '4' for key 1 INSERT INTO 'mytable' (members_id, members_username, members_password,)Filename: C:\AppServ\www\app\system\database\DB_driver.php

Line Number: 330

i use this function and i think i have some logic error .

function count(){
 $query = $this->db->get('mytable');
foreach ($query->result() as $row) {
 if ($row->id > 1) {
  $Right_Child= $this->insert_to_right($row, $row->id);
  $Left_child = $this->insert_to_left($row, $row->id);
$count1=0;
    $count2=0;
if($Left_child!=NULL){

        for($count1;$count1<=10;$count1++)

            {
       $count1=$htis->Left_child->count();
            echo 'Left_child';

        }
        if($Right_Child!=NULL){
            for($count2;$count2<=10;$count2++){
            $count2= $this ->Right_Child->count();

            return $count2;
            echo 'Right_Child';
            if($count1==$count2){
                echo'tree';

            }

            }
        }
    }

        }
3
What is your question? - tjons
how to solve this error message & is this logic for counting number of child correct ? - user3046785
hello, the problem is you've set UNIQUE key for ID (as should be, but you've forgotten set it to Auto Increment.) - Kyslik
I set it to Auto Increment and i got the same error . - user3046785
the problem is when u insert the data.. check ur query for insert .. if u set auto increment to the primary key.. just let the value null for the members_id - Amin Adha

3 Answers

0
votes

What Amin Adha has pointed out is correct.

When you INSERT a record into the database, try to leave out the ID of the record, or, set it to NULL.

In this case, the ID field appears to be "members_id"

You have a problem with an insert somewhere.

0
votes

Try to add auto increamt to member_id

 ALTER TABLE mytable ADD members_id INT PRIMARY KEY AUTO_INCREMENT;

Once remove old data and try again.

0
votes

If you using Integer for id, Try change your type field member_id from int->PRIMARY into varchar->PRIMARY, and dont use Auto Increment on your Database