2014-11-06 44 views
1

我一直在为学校开发一个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变量放在那里,我会得到一个错误,他错过了一个参数,所以我在这里有点困惑。另外,我遇​​到的主要问题是,尽管存在错误和所有问题,但他仍然将记录插入到数据库中......尽管已经有相同电子邮件地址的记录!

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

+0

做的print_r($ _ POST),并告诉我们结果 – 2014-11-06 09:28:54

回答

1

只是一个错误,将电子邮件字段数据分配给一个变量,然后将您的电子邮件传递给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; 
} 
+0

是啊,这做到了!不能相信这很容易,我对这个哈哈失去了主意。非常感谢。 – 2014-11-06 09:40:06

+0

@Jason哈哈哈,有时会发生;) – 2014-11-06 09:43:17

+0

我得到了一个语法错误,但你给我的新查询。我没有看到问题出在哪里,因为它应该是正确的...... “您的SQL语法错误;请检查与您的MySQL服务器版本对应的手册,以找到'ruud @ mail附近使用的正确语法.com''LIMIT 1'at line 2“ 任何想法? – 2014-11-06 09:53:23