如何使用代码点火器PHP签入只有数据值的表?
我有团队成员表和团队表。在团队成员表have中,有三列分别为teamid、staff_id和stafftypeleader或technician。一个团队中只有一个leader staff_type列值。所以,当我向团队成员表插入数据时,我需要检查同一个团队中是否有任何领导 如何显示此团队中已经有领导的错误消息?团队成员表如下所示如何使用代码点火器PHP签入只有数据值的表?,php,mysql,sql-server,codeigniter,Php,Mysql,Sql Server,Codeigniter,我有团队成员表和团队表。在团队成员表have中,有三列分别为teamid、staff_id和stafftypeleader或technician。一个团队中只有一个leader staff_type列值。所以,当我向团队成员表插入数据时,我需要检查同一个团队中是否有任何领导 如何显示此团队中已经有领导的错误消息?团队成员表如下所示 team_id staff_id stafftype 1 2 leader //see here already have l
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
当试图从前端保存时,需要显示错误消息ie;这个团队已经有领导了
模态
控制器
我不完全确定你想用现有的array_搜索做什么,但我猜这是让leader检查正常工作的初步尝试?如果不是这样,请告诉我 您走的是正确的常规路线,首先查询表以检查冲突,只是执行不完全正确 你需要做的是 如果要添加领导,请检查该团队是否已经有领导 如果是这样的话,设法处理它 否则,继续插入 试试下面的方法。输入应该是一个数组,其键对应于数据库列。例如:
$datas = [
'team_id' => 1,
'staff_id' => 3,
'stafftype' => 'leader'
]
值得注意的是,到目前为止,您的代码在编辑和添加之间似乎有点不匹配,这一点在这里还没有提到。此函数假定您只是添加新成员,而不是编辑现有成员。在这种情况下,您需要更多信息。在插入之前执行select查询,以检查您使用的是mysql还是sql server数据库系统的领导者?首先检查$data['Staff\u type']是否为领导者,如果不是,则插入数据,否则检查团队是否有领导者,您只需在select for teamid和status leader中添加where子句,如果结果为空,请插入$data,否则返回1。这是海峡forward@nbk可以编辑我的代码吗?plz@nbk我还需要检查活动状态列。在检查staff_type列中的任何前导时,其中只有状态activeMessage:$staffType=strtolower$datas['staff_type'行出现非法字符串偏移量'staff_type'警告;错误号:42S22[Microsoft][SQL Server的ODBC驱动程序13][SQL Server]列名“1”无效。在team_members中插入1个值,其中第一个值表示传递的是字符串而不是数组,第二个值表示传递的可能是空数组。您需要传入一个关联数组,该数组的键与DB列相匹配扫描u plz编辑我的控制器功能代码???我需要如何传递数据?当我尝试打印$datas时,结果是数组[Staff_id]=>3[Staff_type]=>Leader[team_id]=>1[status]=>active
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');
}
}
}
$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();
}
}