如何使用代码点火器PHP签入只有数据值的表?

我有团队成员表和团队表。在团队成员表中有,三列有teamid、staff_id和stafftype(领导或技术员)。只有一名领导(staff_type 列值)会出现在一个团队中。因此,当我将数据插入团队成员表时,我需要检查同一团队中是否有任何领导者。


如何显示“该团队中已有领导者”的错误信息?团队成员表如下所示,


team_id   staff_id   stafftype

1          2        leader //see here already have leader for team_id 1

2          1        other

3          8        other

1          5        Technician

2          3        Other

1          4        Leader //This should not come. becoz already teamid-1 have Leader 

尝试从前端保存时,需要显示错误消息,即;“该团队已经有领导者”


莫代尔


public function addteam_memb($datas) {

    $this->db->select('*');

    $this->db->from('team_members');

    $querySS = $this->db->get()->result();


    if(array_search('1', array_column($querySS, 'team_id')) !== false) {

        return 1;

    }

    else {

        $insert_id = 0;

        if ( ! empty($datas)) {

            $this->db->insert('team_members', $datas);

            $insert_id = $this->db->insert_id();

        }

    }

}

控制器


public function editteammember() {

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

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

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

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


    //if ( ! empty($getaddstaffname) ) 

    if ( ! empty($getaddstaffname) && ! empty($getaddstafftype) ) {

        foreach ($getaddstaffname as $key => $value ){

            $data['Staff_id'] = $value;

            $data['Staff_type'] = $getaddstafftype[$key];

            $data['team_id'] = $getteamid[$key];

            $data['status'] = "active";

            $value = $this->mastertable_model->addteam_memb($data); 

        }


        if($value == 1) {

            echo "Already have leader in this team";

        } else {

            //$this->load->view('team_memb_creation');        

        }

    } 

}


慕标5832272
浏览 149回答 1
1回答

幕布斯6054654

我不完全确定你想用现有的 array_search 做什么,但我猜这是让领导检查工作的初步尝试?如果不是这样,请告诉我您处于正确的一般轨道上,首先查询表以检查冲突,只是不太正确的执行。你需要做的是如果您要添加领导者,请检查该团队是否已经有领导者如果是这样,请以某种方式处理它否则继续插入尝试类似下面的内容。输入应该是一个数组,其键对应于数据库列。例如:$datas = [    'team_id' => 1,    'staff_id' => 3,    'stafftype' => 'leader']// Its a good idea to use constants for things like thisconst STAFFTYPE_LEADER = 'leader';/** * Add a team member to the database  * * Input array must be an array representing a team member to be added, * with key names that match database column names. * * @param array $datas  * @throws Exception If adding a leader when one already exists */public function addteam_memb($datas) {    // Lower case conversion just for consistency.     $staffType = strtolower($datas['Staff_type']);    // Only run the query if we're trying to add a team lead    // Otherwise there's no need to worry    if ($staffType === self::STAFFTYPE_LEADER) {        $this->db->select('*');        $this->db->from('team_members');        $this->db->where('team_id', $datas['team_id'])        $this->db->where('stafftype', self::STAFFTYPE_LEADER)        if ($this->db->count_all_results() >= 1) {            // Team leader already exists, handle however you would like            // I would typically recommend an exception, but thats up to you!            throw new Exception('Team leader exists');        }        // Reset query before moving on to the insert        $this->db->reset_query();    }    // Either we're not adding a leader, or there is no current leader    // so go ahead with the insert    $insert_id = 0;    if ( ! empty($datas)) {        $this->db->insert('team_members', $datas);        $insert_id = $this->db->insert_id();    }}值得一提的是你的代码到目前为止似乎有一点不匹配的editing和adding尚未在此解决。此功能假定您仅添加新成员,而不是编辑现有成员。对于这种情况,您将需要更多东西。
打开App,查看更多内容
随时随地看视频慕课网APP