为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕标5832272 2021-11-05 16:21:10
我有团队成员表和团队表。在团队成员表中有,三列有teamid、staff_id和stafftype(领导或技术员)。只有一名领导(staff_type 列值)会出现在一个团队中。因此,当我将数据插入团队成员表时,我需要检查同一团队中是否有任何领导者。如何显示“该团队中已有领导者”的错误信息?团队成员表如下所示,team_id   staff_id   stafftype1          2        leader //see here already have leader for team_id 12          1        other3          8        other1          5        Technician2          3        Other1          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');                }    } }
查看完整描述

1 回答

?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

我不完全确定你想用现有的 array_search 做什么,但我猜这是让领导检查工作的初步尝试?如果不是这样,请告诉我

您处于正确的一般轨道上,首先查询表以检查冲突,只是不太正确的执行。

你需要做的是

  • 如果您要添加领导者,请检查该团队是否已经有领导者

  • 如果是这样,请以某种方式处理它

  • 否则继续插入

尝试类似下面的内容。输入应该是一个数组,其键对应于数据库列。例如:

$datas = [

    'team_id' => 1,

    'staff_id' => 3,

    'stafftype' => 'leader'

]

// Its a good idea to use constants for things like this

const 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尚未在此解决。此功能假定您仅添加新成员,而不是编辑现有成员。对于这种情况,您将需要更多东西。


查看完整回答
反对 回复 2021-11-05
  • 1 回答
  • 0 关注
  • 148 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信