0

I am trying to redirect user to some other page when duplicate Entry Error Occurs. I am using Codeigniter

. I have written the following code, but it displays the error message Duplicate entry '1' for key 'semester_id'.

Kindly check the code and guide me.

Thanks

public function insert_sports_sports($student_id, $squash, $football, $cricket, $hockey, $swimming, $semester_id)
{
    $data= array (

            "squash"=> $squash,
            "football"=>$football,
            "cricket"=>$cricket,
            "hockey"=>$hockey,
            "swimming"=>$swimming,
            "studentId"=>$student_id,
            "semester_id" => $semester_id

    );  

    if ($this->db->insert("sports",$data))
    {
        return true;
        }
        else
        {
            return false;
            }
    }

CONTROLLER

---
--
--
    if($this->loginmodel->insert_sports_sports($student_id, $squash, $football, $cricket,
                                             $hockey, $swimming, $semester_id))
    {
        redirect('mycontroller/index');
        }
        else
        {
            $this->load->view('success');
            }
    }   

EDIT

    if ($this->db->insert("sports",$data))
    {
        return true;
        }
        else
        {
            return false;
            }
    }


catch (Exception $ex) {

        if($ex->message == "Duplicate entry '1' for key 'semester_id'")
        {
            redirect('success.php');    

            }       
}
}

ERROR MESSAGE

A Database Error Occurred

Error Number: 1062

Duplicate entry '1' for key 'semester_id'

INSERT INTO `sports` (`squash`, `football`, `cricket`, `hockey`, `swimming`, `studentId`, `semester_id`) VALUES ('4', '4', '4', '4', '4', '42', '1')

Filename: F:\xampp\htdocs\student_management2\system\database\DB_driver.php

Line Number: 330
user3480644
  • 577
  • 4
  • 8
  • 23
  • That essentially means that semester_id is a unique key, meaning that there's already an entry in the database where that column has a value of 1. [This](http://stackoverflow.com/questions/487314/primary-key-or-unique-index/2916464#2916464) page explains it all. – jeremy Jul 07 '14 at 06:27
  • Yes, but i don't want to display this error message, i want to redirect user to some other page and display customized error message – user3480644 Jul 07 '14 at 06:29

3 Answers3

3

If you want only to redirect on an error

try {
    $data= array (

            "squash"=> $squash,
            "football"=>$football,
            "cricket"=>$cricket,
            "hockey"=>$hockey,
            "swimming"=>$swimming,
            "studentId"=>$student_id,
            "semester_id" => $semester_id

    );  

    if ($this->db->insert("sports",$data))
    {
        return true;
        }
        else
        {
            return false;
            }
    }
}
catch (Exception $ex) {
    if($ex->message == "Duplicate entry '1' for key 'semester_id'")
        redirect('yourpage.php');
}
iamawebgeek
  • 2,713
  • 1
  • 18
  • 34
  • 1
    Thanks for the Reply..I am still getting the same error message.. I have updated my question.. Kindly check it again. – user3480644 Jul 07 '14 at 06:51
  • 1
    My bad I have forgotten to access message property of an exception. Change $ex to $ex->message, if does not work just echo $ex->message in catch block than change the message to be compared – iamawebgeek Jul 07 '14 at 06:52
  • 1
    $ex->getMessage() or you can change if($ex->message == "Duplicate entry '1' for key 'semester_id'") to if($ex->getLine() == 330) redirect('page'); though it is not fully correct. – iamawebgeek Jul 07 '14 at 07:05
3

remove try catch and try this

$orig_debug = $this->db->db_debug;
$this->db->db_debug = FALSE;

if ($this->db->insert("sports",$data))
 {
    $this->db->db_debug = $orig_debug;
    return true;
 }
else
 {
   $this->db->db_debug = $orig_debug;
    return false;
 }

it basically mutes the debug for your insert query.in case of error, no insertion takes place hence it will return false.

Karan Thakkar
  • 1,492
  • 2
  • 17
  • 24
2

You can prevent it by setting rule for semester_id as follows

$this->form_validation->set_rules('semester_id', 'input_name', 'trim|required|is_unique[TABLENAME.COLUMNNAME]');
Vinie
  • 2,983
  • 1
  • 18
  • 29