如果记录已存在,则显示消息

时间:2012-05-26 12:34:14

标签: codeigniter

我是 CodeIgniter 的新手。我创建了一个方法,现在在mysql db表中插入记录,如果已存在同名记录,则使查询停止,所以我写道:

public function set_project()
    {
        // If project name already exists?
        $query = $this->db->query("SELECT pro_id FROM projects WHERE pro_name = '".$this->input->post('pro_name')."'");

        if ( sizeof($query->row_array()) == 0) {

            $data = array(
                'is_featured' => $this->input->post('is_featured'),
                'pro_type' => $this->input->post('pro_type'),
                'pro_name' => $this->input->post('pro_name'),
                'pro_client' => $this->input->post('pro_client'),
                'pro_loc_city' => $this->input->post('pro_loc_city'),
                'pro_loc_state' => $this->input->post('pro_loc_state'),
                'pro_yr_start' => $this->input->post('pro_yr_start'),
                'pro_yr_end' => $this->input->post('pro_yr_end'),
                'pro_loc_cur' => $this->input->post('pro_loc_cur'),
                'pro_size' => $this->input->post('pro_size'),
                'pro_size_cat' => $this->input->post('pro_size_cat'),
                'pro_size_units' => $this->input->post('pro_size_units'),
                'pro_desc' => $this->input->post('pro_desc')
            );

            return $this->db->insert('projects', $data);

        } else {
            return "Already exists";
        }

工作正常,但我可以向观众展示消息?

2 个答案:

答案 0 :(得分:0)

好的,我做到了,实际上我必须在控制器中移动该检查。

答案 1 :(得分:0)

您正在撰写return "Already exists",但要查看输出会将该代码更改为echo '<script>alert("Already exists!")</script>';