Jagadeesh
Jagadeesh

Reputation: 754

While refreshing, duplicate data inserts into DB in codeigniter

I have inserted data into my table using $this->db->insert(). When the data inserted successfully the message have to display, also the data should not insert again.

controller:

public function index($message = '') {
    $this->load->template('homePage');
}

/**
 * Insert functionlaity
 */
public function insert() {
    if ($_POST['save']) {
        $result = $this->home->insertEntry();
        $data['message'] = ($result > 0) ? 'saved' : 'Not';
        redirect('HomeController',$data);
    }
    $this->load->view('homePage');
}

Model:

public function insertEntry()
{
    $this->name    = $_POST['name'];
    $this->email   = $_POST['email'];
    $this->db->insert('users', $this);
    return $this->db->affected_rows();

}

View:

    <div class="container sampleForm"><?php
echo isset ($message) ? $message : '';
echo form_open( get_class(get_instance()) . '/insert')
 . form_label('id') . form_input('id', isset($query['id']) ? $query['id'] : '', 'class="form-control"') . br()
 . form_label('name') . form_input('name', isset($query['name']) ? $query['name'] : '', 'class="form-control"') . br()
 . form_label('email') . form_custom_input('email','email', isset($query['email']) ? $query['email'] : '', 'class="form-control"') . br()
 . form_submit('save', 'save', 'class="btn btn-primary"')
 . form_close();
?> </div>

My issue is if i can display status then data insert for every refresh else status will not be displayed.

This all should works in my HomeController.php as controller, Home.php as model and homePage.php as view file. Not in any other files.

My logic is:

Ex: I have a student table in that i am gonna to store student names and marks like name, m1, m2, m3. Now I want to add marks if its added i want to display the status. Duplicate insertion should not happen while refresh the page. The student name and marks were similar. I want to do in php alone.

Upvotes: 4

Views: 3690

Answers (5)

Ankit Verma
Ankit Verma

Reputation: 136

In Controller :

public function inserttest() {
   if (isset($_POST['submit'])) {
       $result = $this->admin_model->insertEntry();     
       if($result>0) {
          // values are saved in session to display the message.
          $this->session->set_flashdata('message', 'Save');
       } else {
          $this->session->set_flashdata('message', 'No');
       }
       // To avoid duplicate redirect to another page
       redirect('admin/user/inserttest');
   } else {
       $this->load->view('admin/test');
   }
}

Model:

public function insertEntry()
{
     $data=array(

     'id'=>$_POST['id'],
     'name'=>$_POST['name'],
     'email'=>$_POST['email'],

     );


    $this->db->insert('test_check',$data);
    return $this->db->affected_rows();

}

View:

<html>
    <body>
       <?php  if(isset($this->session->flashdata('message'))){ 
              echo $this->session->flashdata('message'); 
              }
       ?>
  <form action="" method="post">

    Id:<input type="text" name="id">

    Name:<input type="text" name="name">
    Email<input type="email" name="email">
    <input type="submit"  value="submit"  name="submit">

  </form>
</body>
</html>

read this for more about flashdata: ellislab.com/codeigniter/user-guide/libraries/sessions.html

Upvotes: 1

vinoth
vinoth

Reputation: 106

Currently, There is no option to pass the status(success, error) messages to the overview page(or desired page).

Use the $_SESSION to holds the values and retrieve values from the session.

To show the status Message and avoid the duplicate entries, The code as follows:

CONTROLLER:

if (isset($_POST['save'])) {
    $result = $this->home->insertEntry();
    $message = ($result > 0) ? 'saved' : 'Not';
    $this->session->set_userdata('message', $message);
    //redirect, avoid duplicate entries
    redirect('HomeController');
}

VIEW:

<div class="container sampleForm"><?php
    if($this->session->has_userdata('message')){
        echo $this->session->message;
        $this->session->unset_userdata('message');
    }

    echo form_open( get_class(get_instance()) . '/insert')
     . form_label('id') . form_input('id', isset($query['id']) ? $query['id'] : '', 'class="form-control"') . br()
     . form_label('name') . form_input('name', isset($query['name']) ? $query['name'] : '', 'class="form-control"') . br()
     . form_label('email') . form_custom_input('email','email', isset($query['email']) ? $query['email'] : '', 'class="form-control"') . br()
     . form_submit('save', 'save', 'class="btn btn-primary"')
     . form_close();
    ?> </div>

Upvotes: 1

Ankit Verma
Ankit Verma

Reputation: 136

$result = $this->home->insertEntry();
$data['message'] = $result ? 'saved' : 'Not';
$this->load->view('Your View page name ',$data);

and use the below code in you view page to display message

if(isset($message))
{
  echo $message;
}

Upvotes: -1

Ankit Verma
Ankit Verma

Reputation: 136

Call this function to open the view page as well as on submit, leave the action="" it checks that you submit the data or only access the view page, but this data will not inset on refresh

function add()
{
   if(isset($_POST{['submit']))
    {
     $result = $this->home->insertEntry();
     $data['message'] = $result ? 'saved' : 'Not';  
     redirect('add',$data);

    }

    $this->load->view('view page');  

}

Upvotes: 0

Mukesh S
Mukesh S

Reputation: 377

Simple you can use below code to resolved the duplicate entry for email

if(!empty($submit))
{
    //echo "<pre>"; print_r($_POST); die;
    $data = $this->get_data_from_post();
    $unique_email = '|is_unique['.TBL_USERS.'.email]';
    $this->form_validation->set_rules('firstname', 'First Name', 'trim|required|min_length[2]|max_length[12]|xss_clean|htmlentities|prep_for_form|alpha');
    $this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email|htmlentities|prep_for_form'.$unique_email);
    if($this->form_validation->run($this) == TRUE ){ 
        $data['name']    = $_POST['name'];
        $data['email']   = $_POST['email'];
        $res = $this->mdl_customer->_insert($data);
        redirect('home'); // where you want to redirect.
    }
}

By doing this the user who refreshes will be refreshing landing_page.php which means it won't do the insert twice. best advice: do a check to see if user exists first if so don't insert!

Once an insert or update is done in your code you always need to redirect to another page.

Upvotes: 0

Related Questions