php - 关于 Php codeigniter 插入查询的建议

标签 php mysql codeigniter

我有一个表格,它已经被窃听了一段时间,当我在表格中填写所有信息时,它似乎很快就会插入它,但是如果一个或多个字段为空,它根本不起作用 我用来插入数据库的查询是。我想弄清楚我是否可以使用 Insert_delay,也许它会解决问题?我需要做的任何可能的解决方案或更改

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['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']   = $this->input->post('password');
        $data['year']       = $this->input->post('year');
        $data['rate']   = $this->input->post('rate');
        $data['class_id']   = $this->input->post('class_id');
        $data['class_id2']  = $this->input->post('class_id2');
        $data['class_id3']  = $this->input->post('class_id3');
        $data['class_id4']  = $this->input->post('class_id4');
        $data['parent_id']  = $this->input->post('parent_id');
        $data['roll']       = $this->input->post('roll');
        $this->db->insert_('student', $data);
        $student_id = $this->db->insert_id();

        $this->session->set_flashdata('flash_message' , get_phrase('Student_added_successfully'));
        $this->email_model->account_opening_email('student', $data['email']); //SEND EMAIL ACCOUNT OPENING EMAIL
        redirect(base_url() . 'index.php?admin/student_add/' . $data['class_id'], 'refresh');

最佳答案

在 Controller 中

function student($param1, $param2 $param3 )
{
    if ($this->session->userdata('admin_login') != 1)
    {
        redirect('login', 'refresh');
    }
    else
    {
        if($param1 == 'create') {

            $result = $this->model_name->insert_data();

            if ($result != FALSE) {

                $email => $this->input->post('email');                  
                $confirm = $this->email_model->account_opening_email('student', $email); # do same as insert

                if ($confirm != FALSE) {

                    $this->session->set_flashdata('flash_message' , get_phrase('Student_added_successfully'));
                    redirect(base_url() . 'index.php?admin/student_add/' . $data['class_id'], 'refresh');
                }
                else
                {
                    echo "Mail send failed";
                }

            }
            else
            {
                echo "param1 is not create";
            }     
        }
    }
}

在模型中

public function insert_data()
{
    # to isert data, I add them in to one array
    $data = array(
        'name' => $this->input->post('name'),
        'sex' => $this->input->post('sex'),
        'address' => $this->input->post('address'),
        'phone' => $this->input->post('phone'),
        'email' => $this->input->post('email'),
        'password' => $this->input->post('password'),
        'year' => $this->input->post('year'),
        'rate' => $this->input->post('rate'),
        'class_id' => $this->input->post('class_id'),
        'class_id2' => $this->input->post('class_id2'),
        'class_id3' => $this->input->post('class_id3'),
        'class_id4' => $this->input->post('class_id4'),
        'parent_id' => $this->input->post('parent_id'),
        'roll' => $this->input->post('roll')
    );

    if (!$this->db->insert('student', $data)) {

        # insert FAILED
        return FALSE;
    }
    else{

        # insert SUCCESS        
        $slast_id = $this->db->insert_id(); # get last insert ID
        return = $slast_id;
    }

}

关于php - 关于 Php codeigniter 插入查询的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33553638/

相关文章:

mysql - 使用 WHERE IN 子句按顺序抓取行

javascript - 使用 JavaScript 将新行添加到表时重置输入字段

php - 如何在不让用户下载字体的情况下嵌入字体?

javascript - 当 AJAX 响应来自 PHP 文件时,如何在中心显示带有消息的加载器图像并防止引导模式对话框关闭?

php - 无法使用 html、php 和 mysql 填充下拉菜单

php - MySql PHP 结果

php - 用php计算不同数据库的产品总价

Java/Android JSON 请求不更新收到的数据

php - GROUP BY 与事件记录?

css - Code Igniter .otf 自定义字体