Check if registration already exists (Codeigniter)

Asked

Viewed 329 times

0

I am developing a scheduling system (automotive sector). The idea is for the user to choose an installer(ComboBox), Schedule(ComboBox) these times are already predefined and the date(Input/Date).

I am trying to create a condition where, if the installer already has scheduling at that date and time, there is no possibility to reschedule (duplicate), and display a warning message.

CONTROLLER:

$dados = array(
                'tecAg' => $this->input->post('tecAg'),
                'horaAgIns' => $this->input->post('horaAgIns'),
                'data' => $this->input->post('data')
            );

            $this->load->model('agendamentos_model');

             if(!$this->agendamentos_model->verifica($dados['horaAgIns' && 'tecAg' && 'data'])) {

                echo 'Horário já utilizado!'; 

                } else {

                $data = array(
                    'tecAg' => set_value('tecAg'),
                    'horaAg' => set_value('horaAg'),
                    'data' => set_value('data'),

MODEL:

public function verifica($tecAg, $horaAgIns, $data){
    $this->db->where('tecAg', $tecAg);
    $this->db->where('horaAgIns', $horaAgIns);
    $this->db->where('data', $data);

    return $this->db->get('agendamentos')->row_array();
}

1 answer

0

Using this Codeigniter select library makes it very difficult in my opinion, try the following :

To get started put this line of code into your controller’s Construct

$this->load->model('agendamentos_model','agendamentos'); 

To make it easier to use your model in the controller, just call it as "scheduling"

Now let’s get to your problem, in my view, you can simply put it this way, without doing checks before the scheduling already exists, just do the Insert using the on Conflict:

$sql = "insert into tabela_agendamento(tecAg, horaAgIns, data) 
values (?, ?, ?) on conflict(data) do nothing"; 

In the above entry (Please change the table name to the correct one, the part of the code "on Conflict", says that if any record already exists with the same date, it will not enter the record, now just run.

$this->db->insert($sql, [$tecAg, $horaAgIns, $data]);

If you don’t use $this->db, just enable in the application/config/autoload.php putting 'database' within the array of $autoload['Ibraries'].

  • First, thank you very much for the reply Vitorzf.

  • My idea is that these three fields (tecAg, timeAgIns, date) receive this data via form input and make this query in the database before saving the records, there are more fields in the form but the idea was only to let save if these times are available. There is already an Add() function. So I tried to use an If inside this same function. @Vitorzf

  • In this case you can do it this way, https://repl.it/repls/GhostwhiteColdWorkplace

  • I got it here, using the same IF, it was just the logic that was wrong. Thank you so much for the strength @Vitorzf

Browser other questions tagged

You are not signed in. Login or sign up in order to post.