CodeIgniter:检查记录是否存在


CodeIgniter: checking if record exists

我一直在为学校做CodeIgniter项目,我需要用用户数据创建一个票证。如果用户还不在数据库中,他在表单中填写的数据将被插入数据库中,如果他已经存在于数据库中,则只使用他已经存在的用户id创建一个票证。

我一直在尝试检查一个用户的电子邮件是否存在,但我运气不好。在谷歌上搜索了几个小时;试过的回调,正常的验证,你能想到的。我只是似乎无法让它发挥作用,我真的很感激你的帮助。这是我当前的代码:

控制器

public function create_ticket() {
    $this->load->library('form_validation');
    // field name, error message, validation rules
    $this->form_validation->set_rules('voornaam', 'Voornaam', 'trim|required');
    $this->form_validation->set_rules('tussenvoegsel', 'Tussenvoegsel', 'trim');
    $this->form_validation->set_rules('achternaam', 'Achternaam', 'trim|required');
    $this->form_validation->set_rules('email', 'E-mailadres', 'trim|required|valid_email');
    $this->form_validation->set_rules('geboortedatum', 'Geboortedatum', 'required');
    $this->form_validation->set_rules('postcode', 'Postcode', 'trim|required');
    $this->form_validation->set_rules('woonplaats', 'Woonplaats', 'trim|required');
    if($this->form_validation->run() == FALSE)
    {
        echo "Failed";
        $this->reservate();
    }
    else
    {
        $this->ticket_model->add_ticket($email);
        $this->reservate();
    }
}

型号:

public function add_ticket($email) {
    $query = $this->db->query("SELECT email FROM visitor
                               WHERE email = '".$email."'");
    if($query->num_rows() == 0){
        $data = array(
            'voornaam' => $this->input->post('voornaam'),
            'tussenvoegsel' => $this->input->post('tussenvoegsel'),
            'achternaam' => $this->input->post('achternaam'),
            'email' => $this->input->post('email'),
            'geboortedatum' => $this->input->post('geboortedatum'),
            'postcode' => $this->input->post('postcode'),
            'woonplaats' => $this->input->post('woonplaats')
        );
        $this->db->insert('visitor', $data);
    }
    else {
        return false;
    }
    }

视图(如果非常重要)

<?php echo validation_errors(); ?>
<h1>Ticket Bestellen</h1>
<label>
    <span>Voornaam</span>
    <input type="text" class="input_text" name="voornaam" id="voornaam"/>
</label>
<label>
    <span>Tussenvoegsel</span>
    <input type="text" class="input_text" name="tussenvoegsel" id="tussenvoegsel"/>
</label>
<label>
    <span>Achternaam</span>
    <input type="text" class="input_text" name="achternaam" id="achternaam"/>
</label>
<label>
    <span>E-mailadres</span>
    <input type="text" class="input_text" name="email" id="email"/>
</label>
<label>
    <span>Geboortedatum</span>
    <input type="text" id="geboortedatum" name="geboortedatum" placeholder="dd-mm-jjjj">
</label>
<label>
    <span>Postcode</span>
    <input type="text" class="input_text" name="postcode" id="postcode"/>
</label>
<label>
    <span>Woonplaats</span>
    <input type="text" class="input_text" name="woonplaats" id="woonplaats"/>
</label>
    <input type="submit" class="button" value="Reserveren" />
</label>

我得到的错误是:

A PHP Error was encountered

严重性:通知

消息:未定义的变量:电子邮件

文件名:controllers/booking.php

线路编号:42

我不知道为什么会发生这种情况,因为如果我不把$email变量放在那里,我会得到一个错误,他错过了一个论点,所以我在这里有点困惑。此外,我遇到的主要问题是,尽管它给出了错误和所有内容,但他还是将记录插入数据库。。。尽管已经存在一个具有相同电子邮件地址的记录!

我真的不知道如何解决这个问题,所以我们非常感谢您的帮助。提前谢谢。

只有一个错误,将电子邮件字段数据分配给一个变量,然后将您的电子邮件传递给add_ticket模型。就是这样:)

$email = $this->input->post("email");
$this->ticket_model->add_ticket($email);

现在修改你的模型,只是一点点,

function add_ticket($email) {
    // Added $this->db->escape() and limit 1 for Performance
    $query = $this->db->query("SELECT email FROM visitor
                           WHERE email = ".$this->db->escape($email)." limit 1");
   // Collect Data Array
   $data = array(
        'voornaam' => $this->input->post('voornaam'),
        'tussenvoegsel' => $this->input->post('tussenvoegsel'),
        'achternaam' => $this->input->post('achternaam'),
        'email' => $this->input->post('email'),
        'geboortedatum' => $this->input->post('geboortedatum'),
        'postcode' => $this->input->post('postcode'),
        'woonplaats' => $this->input->post('woonplaats')
    );
 // Make Code Short and Clear
 return $query->num_rows() == 0 ? $this->db->insert('visitor', $data) : false;
}