检查db row是否有特定值

relj7zay  于 2021-06-24  发布在  Mysql
关注(0)|答案(3)|浏览(342)

我正在创建一个事件,每个事件将有与会者,我得到了这个工作罚款,问题是我有一个与会者可以参加同一事件两次,这是不好的。
事件视图

<?php if($this->session->userdata('user_id')): ?>
<hr>
<?php echo form_open('/attendees/add/'.$event['id']); ?>
    <input type="hidden" name="user_id" value="<?php echo $_SESSION['user_id']; ?>">
    <input type="submit" value="I'm in!" class="btn btn-success">
</form>
<?php endif; ?>

与会者控制器

<?php
class Attendees extends CI_Controller {
    public function add($event_id) {
        // Check login
        if(!$this->session->userdata('logged_in')){
            redirect('users/login');
        }
        $this->form_validation->set_rules('user_id', 'required|callback_check_userid_eventid');
        if($this->form_validation->run() === FALSE){
            $this->session->set_flashdata('attendee_not_added', 'You are already on the list.');
            redirect('home');
        } else {
            $this->attendee_model->add_attendee($event_id);
            // Set message
            $this->session->set_flashdata('attendee_added', 'You have been added to this event.');
            redirect('events');
        }
    }
}

与会者模型

<?php
class Attendee_model extends CI_Model {
    public function __contruct() {

    }

    public function get_attendees($id = FALSE){
        if($id === FALSE) {
            $query = $this->db->get('attendees');
            return $query->result_array();
        }
        $this->db->select('attendees.id, attendees.team, attendees.is_goalie, event.id, user.first_name, user.last_name');
        $this->db->from('attendees');
        $this->db->join('event', 'attendees.event_id = event.id', 'inner');
        $this->db->join('user', 'attendees.user_id = user.id', 'inner');
        $this->db->where('event.id', $id);
        $query = $this->db->get();
        return $query->row_array();
    }

    public function add_attendee($event_id){
        $data = array(
            'event_id' => $event_id,
            'user_id' => $this->session->userdata('user_id')
        );
        return $this->db->insert('attendees', $data);
    }

    // Check attendee exists in event
    public function check_userid_eventid($event_id, $user_id){
        $query = $this->db->get_where('attendees', array('user_id' => $user_id, 'event_id' => $event_id));
        if(empty($query->row_array())){
            return true;
        } else {
            return false;
        }
    }
}

如您所见,我尝试在按钮窗体验证上创建一个自定义回调,但没有成功。
如果有人能给我指出正确的方向,让我知道我是否有点接近。
提前谢谢。

tjjdgumg

tjjdgumg1#

你没有通过考试 $event_id 回调函数的值。用以下行替换验证

$this->form_validation->set_rules('user_id', 'User ID', 'required|callback_check_userid_eventid['.$event_id.']');

在attendees控制器文件中添加以下回调函数

public function check_userid_eventid($user_id, $event_id){
    $CI =& get_instance();
    $CI->load->database();
    $CI->form_validation->set_message('user_id_unique', "Sorry, that %s is already being used.");
    return isset($CI->db) ? ($CI->db->limit(1)->get_where('attendees',compact('user_id','event_id'))->num_rows() == 0) : false;
}
xdnvmnnf

xdnvmnnf2#

将回调函数放入 Controller ```

qyswt5oh

qyswt5oh3#

您的回调所处的模型对表单验证一无所知。如果您查看docs:callbacks,它应该与使用它的表单验证方法位于同一个控制器中。
但是,可以将模型中的函数用作具有不同语法的回调:

$this->form_validation->set_rules(
        'username', 'Username',
        array(
                'required',
                array($this->users_model, 'valid_username')
        )
);

如本文所述。
最后,如果返回值为false,则需要确保设置的消息如下例所示:

public function username_check($str)
        {
                if ($str == 'test')
                {
                        $this->form_validation->set_message('username_check', 'The {field} field can not be the word "test"');
                        return FALSE;
                }
                else
                {
                        return TRUE;
                }
        }

总之:文档中有所有的答案。

相关问题