codeigniter添加数据库ss

时间:2014-05-09 13:50:40

标签: php codeigniter

我对CodeIgniter有疑问 当我添加一个交付请求并且我在添加时填充后者的字段时,我在数据库中放置了一个属性,默认情况下为0,自动添加,但由于我无法添加请求

如何添加应用程序并传递值,每次添加时都会自动添加值为零的数据库

以下是我使用的一些代码:

function add($name, $email, $phone, $depart, $arrivee, $capacite, $livrer, $uid)
{
    $query = $this->db->get_where('contacts', array(
        'name' => $name,
        'uid' => $uid
    ));
    if ($query->num_rows == 1)
    {
        return FALSE;
    }

    $this->db->insert('contacts', array(
        'name' => $name,
        'email' => $email,
        'phone' => $phone,
        'depart' => $depart,
        'arrivee' => $arrivee,
        'capacite' => $capacite,
        'livrer' => 0,
        'uid' => $uid
    ));
    $this->db->set('contacts', 'contacts+1', FALSE)->where('uid', $uid)->update('users');
    return TRUE;
}

}

public function add_contact()
{
    sleep(1);
    $this->load->library('form_validation');
    $this->form_validation->set_rules('name', 'Name', 'required|max_length[40]|callback_alpha_dash_space');
    $this->form_validation->set_rules('email', 'Email', 'required|max_length[40]|valid_email');
    $this->form_validation->set_rules('phone', 'Phone', 'required|max_length[8]|alpha_numeric');
    $this->form_validation->set_rules('depart', 'Depart', 'required|max_length[15]|trim');
    $this->form_validation->set_rules('arrivee', 'Arrivee', 'required|max_length[15]|trim');
    $this->form_validation->set_rules('capacite', 'Capacite', 'required|max_length[15]|trim');


    if ($this->form_validation->run() == FALSE) {
        $message = "<strong>ajout</strong> echoué!";
        $this->json_response(FALSE, $message);
    } else {
        $is_added = $this->contact_model->add($this->input->post('name'), $this->input->post('email'), 
            $this->input->post('phone'),$this->input->post('depart'),$this->input->post('arrivee'),$this->input->post('capacite'), $this->session->userdata('uid'));

        if ($is_added) {
            $message = "<strong>".$this->input->post('name')."</strong> a été ajouté!";
            $this->json_response(TRUE, $message);
        } else {
            $message = "<strong>".$this->input->post('name')."</strong> existe deja!";
            $this->json_response(FALSE, $message);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

检查您的add方法是返回true还是false。如果联系人不存在,它看起来不会继续。

来自您的控制器:

if($this->your_model->add([...]) == TRUE) {
 echo 'This user was added';
}
else {
 echo 'The user didn\'t exist.';
}

未检查您的代码是否确实连接到您的数据库并且表结构是否有效。