0

I am using Codeigniter and I have created a code that checks if there is the same entry already in the database. but i dont know how i will output the error message. the boolean is not working.

VIEW

<h8><b>Add New Service: For single upload. <?php echo $status; ?></b></h8><hr>
                        <form action="<?php echo base_url(); ?>some_controller/insertServ" method="post">
                                    <center>Service Name: <input type="text"  name="ci_name"/>
                                    <input type="submit" class="classname" value="Save"/></center>
                        </form>

CONTROLLER

public function insertServ(){
    /* logic behind adding a new service
     */

        $ci_name = $this->input->post('ci_name');

        $success = $this->some_model->addCI($ci_name);

        if($success == TRUE)
            $this->viewMap_add(TRUE);
        else $this->viewMap_add(FALSE);


}

public function viewMap_add($success = NULL){
    /* Shows the list of the services with a dialog box for 
     * adding a new service
     */
     if($success == NULL)
        $status = 'N/A';
    else if($success == TRUE)
        $status = 'Success';
    else $status =  'FAILED';

    $data['status'] = $status;
        $data['current_user']=$this->session->userdata('email');
        $data['mapList'] = $this->some_model->getMapped();
        $this->load->view('templates/header.php',$data);
        $this->load->view('some_page/servList_add.php',$data);

}

MODEL

public function addCI($ci_name){
    /* Adds a new service
     */
    $ci_name = $this->db->escape_str($ci_name);

    $queryStr = "Select service from appwarehouse.service where service = '$ci_name'";
    $query = $this->db->query($queryStr);
    if($query->num_rows()>0){
       echo "result already exists";
     }
     else{
    $queryStr = "INSERT INTO appwarehouse.service(service) VALUES ('$ci_name');"; 
    $query = $this->db->query($queryStr);}

}

tereško
  • 58,060
  • 25
  • 98
  • 150

1 Answers1

0

First off, your model method addCI isn't returning anything. Fix that.

Then, you can avoid all the mess by simply removing one layer of code and sending the status value directly:

public function insertServ:

if($success)
    $this->viewMap_add('Success');
else 
    $this->viewMap_add('FAILED');

And then just remove all the if-elses in the viewMap_add method.

Also, since you didn't tell exactly what is the problem, try doing the var_dump on the status variable in insertServ or just:

var_dump($this->some_model->addCI($ci_name));

UPDATE

Here is how your model method should look like (nothing to echo there, but return a boolean):

public function addCI($ci_name){
    /* Adds a new service
     */
    $ci_name = $this->db->escape_str($ci_name);

    $queryStr = "Select service from appwarehouse.service where service = '$ci_name'";
    $query = $this->db->query($queryStr);
    if($query->num_rows()>0){
        return false; // <---- this one is important
    }
    else{
        $queryStr = "INSERT INTO appwarehouse.service(service) VALUES ('$ci_name');"; 
        $query = $this->db->query($queryStr);
        return true; // <---- and this one
    }
}
Shomz
  • 37,421
  • 4
  • 57
  • 85
  • it always displays success. how will i echo out that there is a duplicate entry. – user3678150 May 27 '14 at 03:04
  • In the insertServ? How come? Your model method isn't returning anything. – Shomz May 27 '14 at 03:06
  • why is it not returning false :( – user3678150 May 27 '14 at 03:38
  • You didn't tell it to return anything. I've added the whole model method to my answer like 30 mins ago. – Shomz May 27 '14 at 03:40
  • yes but i've copied it to my code but it is returning null instead of failed. maybe because i declared it as null in the argument? – user3678150 May 27 '14 at 04:01
  • you can do this with the form_validation and write a callback_function to validate the database existence entry of your post. – Bira May 27 '14 at 04:35
  • null is the default value if no other value is set, that's not the problem. See what your rendered query looks like, see what the result is, and try to manually run the query against the database to check whether you get the desired results. – Shomz May 27 '14 at 12:14