简体   繁体   English

CodeIgniter:检查记录是否存在

[英]CodeIgniter: checking if record exists

I've been working on a CodeIgniter project for school and I need to create a ticket with user data. 我一直在为学校的CodeIgniter项目工作,我需要使用用户数据创建票证。 If the user is not yet in the database, the data he filled into the form gets inserted into the database and if he already exists in the database, just a ticket is created with his already existing user id. 如果用户尚未在数据库中,则将他填写到表单中的数据插入数据库中,如果他已存在于数据库中,则仅使用其现有用户ID创建票证。

I've been trying to check if a user exists with his e-mail, but I've had no luck. 我一直在尝试检查用户的电子邮件是否存在,但是我没有运气。 Googled for hours; 搜索了几个小时; tried callbacks, normal validations, you name it. 尝试过的回调,常规验证,您都可以命名。 I just can't seem to get it to work and would really appreciate some help. 我只是似乎无法使它正常工作,因此非常感谢您的帮助。 Here's my current code: 这是我当前的代码:

Controller 调节器

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();

    }
}

Model: 模型:

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;
    }

    }

View (if at all important) 查看(如果很重要)

<?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>

The error I'm getting is: 我得到的错误是:

A PHP Error was encountered

Severity: Notice 严重程度:注意

Message: Undefined variable: email 消息:未定义的变量:电子邮件

Filename: controllers/booking.php 文件名:controllers / booking.php

Line Number: 42 行号:42

I don't know why this is happening because if I don't put the $email variable there, I get the error that he's missing an argument so I'm a little confused here. 我不知道为什么会这样,因为如果我不把$ email变量放在那里,我会得到一个错误,他缺少一个参数,所以我在这里有些困惑。 Also, the main problem I have is that, despite it giving errors and everything, he inserts the record into the database anyway... Despite a record with the same e-mail address already being there! 另外,我遇​​到的主要问题是,尽管它给出了错误和所有提示,但他仍然将记录插入数据库中……尽管一条记录中包含相同的电子邮件地址!

I really don't know how to fix this, so any help is greatly appreciated. 我真的不知道如何解决此问题,因此非常感谢您的帮助。 Thanks in advance. 提前致谢。

Just one mistake, Assing Email field data to a variable, then pass your email to the add_ticket model. 只是一个错误,将电子邮件字段数据关联到变量,然后将您的电子邮件传递给add_ticket模型。 Thats it :) 而已 :)

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

Now Modify Your Model, Just a little, 现在,只需一点点修改模型,

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM