学生已经存在对入学系统的验证(Codeigniter)

时间:2014-09-09 09:37:39

标签: javascript php sql codeigniter validation

我是编码的新手,我需要一些帮助,如何在添加学生时进行验证,它会提示学生已经存在,我在这里有一个示例代码来创建学生和更新。但我不知道如果学生已经存在,当我添加一个具有相同全名的学生时,如何验证它。如果电子邮件已经存在,我也需要验证我的电子邮件..

提前谢谢..希望你帮了我很多..:D

function student($param1 = '', $param2 = '', $param3 = '')
{
    if ($this->session->userdata('admin_login') != 1)
        redirect('login', 'refresh');
    if ($param1 == 'create') {
        $data['name']        = $this->input->post('name');
        $data['birthday']    = $this->input->post('birthday');
        $data['sex']         = $this->input->post('sex');
        $data['address']     = $this->input->post('address');
        $data['phone']       = $this->input->post('phone');
        $data['email']       = $this->input->post('email');
        $data['password']    = md5($this->input->post('password')); 
        $data['father_name'] = $this->input->post('father_name');
        $data['mother_name'] = $this->input->post('mother_name');
        $data['class_id']    = $this->input->post('class_id');
        $data['roll']        = $this->input->post('roll');
        $this->db->insert('student', $data);
        $student_id = mysql_insert_id();
        move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/student_image/' . $student_id . '.jpg');
        $this->email_model->account_opening_email('student', $data['email']); //SEND EMAIL ACCOUNT OPENING EMAIL
        $this->session->set_flashdata('flash_message', get_phrase('add_student_success'));
        redirect(base_url() . 'index.php?admin/student/' . $data['class_id'], 'refresh');
    }
    if ($param2 == 'do_update') {
        $data['name']        = $this->input->post('name');
        $data['birthday']    = $this->input->post('birthday');
        $data['sex']         = $this->input->post('sex');
        $data['address']     = $this->input->post('address');
        $data['phone']       = $this->input->post('phone');
        $data['email']       = $this->input->post('email');
        $data['password']    = md5($this->input->post('password'));
        $data['father_name'] = $this->input->post('father_name');
        $data['mother_name'] = $this->input->post('mother_name');
        $data['class_id']    = $this->input->post('class_id');
        $data['roll']        = $this->input->post('roll');

        $this->db->where('student_id', $param3);
        $this->db->update('student', $data);
        move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/student_image/' . $param3 . '.jpg');
        $this->crud_model->clear_cache();
        $this->session->set_flashdata('flash_message', get_phrase('edit_student_success'));
        redirect(base_url() . 'index.php?admin/student/' . $param1, 'refresh');
    } else if ($param2 == 'edit') {
        $page_data['edit_data'] = $this->db->get_where('student', array(
            'student_id' => $param3
        ))->result_array();
    } else if ($param2 == 'personal_profile') {
        $page_data['personal_profile']   = true;
        $page_data['current_student_id'] = $param3;
    } else if ($param2 == 'academic_result') {
        $page_data['academic_result']    = true;
        $page_data['current_student_id'] = $param3;
    }
    if ($param2 == 'delete') {
        $this->db->where('student_id', $param3);
        $this->db->delete('student');
        redirect(base_url() . 'index.php?admin/student/' . $param1, 'refresh');
    }
    $page_data['class_id']   = $param1;
    $page_data['students']   = $this->db->get_where('student', array(
        'class_id' => $param1
    ))->result_array();
    $page_data['page_name']  = 'student';
    $page_data['page_title'] = get_phrase('manage_student');
    $this->load->view('index', $page_data);
}

2 个答案:

答案 0 :(得分:1)

请用这个。

$firstName  = $this->input->post('firstName');
$lastName   = $this->input->post('lastName');

$checkStudentExists = $this->db->get_where('student', array('lastName' => $lastName, 'firstName' => $firstName);

if(count($checkStudentExists) > 0) {
  echo "Student already exists. Please enter another.";
} else {
# Do your code
} 

希望它有所帮助。他们有许多姓氏和名字共存,请添加中间名或生日进行验证

答案 1 :(得分:0)

如果全名已经存在,我会问我的验证码是否正确..

$name  = $this->input->post('name');

$checkFullnameExists = $this->db->get_where('student', array('name' => $name,);
if(count($checkStudentExists) > 0) {
$this->session->set_flashdata('flash_message', get_phrase('Student is already exists'));
} else {
#do your code
} 
如果电子邮件已经存在,我还需要验证电子邮件还有一件事......非常感谢@Shudmeyer ......