我一直在为学校开发一个CodeIgniter项目,我需要创建一个包含用户数据的故障单。如果用户还没有在数据库中,他填入表单的数据会被插入到数据库中,并且如果他已经存在于数据库中,那么只需使用他已经存在的用户标识创建票证。CodeIgniter:检查是否存在记录,尝试了一切
我一直在试图检查用户是否存在他的电子邮件,但我没有运气。 Google搜索几个小时;尝试回调,正常验证,你的名字。我似乎无法得到它的工作,并会非常感谢一些帮助。这里是我当前的代码:
控制器
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
严重性:注意
消息:未定义的变量:电子邮件
文件名:控制器/ booking.php
行号:42
我不知道为什么会发生这种情况,因为如果我不把$ email变量放在那里,我会得到一个错误,他错过了一个参数,所以我在这里有点困惑。另外,我遇到的主要问题是,尽管存在错误和所有问题,但他仍然将记录插入到数据库中......尽管已经有相同电子邮件地址的记录!
我真的不知道如何解决这个问题,所以任何帮助,非常感谢。提前致谢。
做的print_r($ _ POST),并告诉我们结果 – 2014-11-06 09:28:54