/**
* 删除更新数据库字段
* @param $table
* @param $field
* @param $teacherId
* @param $idArr
* @param $sqlOptions sql条件字段 --查询
*/
public function updateField($table, $field, $teacherId, $idArray, $sqlOptions = ‘‘)
{
$is = M("{$table}")->find("$sqlOptions = ?", [$teacherId])->first();
if ($is) {
$delSql = " delete from {$table} where teacher_id = ? ";
M("{$table}")->delete();
}
$idArr = explode(‘,‘, $idArray);
$insertSql = "insert into {$table} ({$field}) values ";
array_walk($idArray, function ($val, $k) use (&$teacherId, &$insertSql, &$field) {
$fieldArr = explode(‘,‘, $field);
if (count($fieldArr) > 2) {
$insertSql .= "({$teacherId},2,{$val}),";
} else {
$insertSql .= "({$teacherId},{$val}),";
}
});
$insertSql = substr($insertSql, 0, strrpos($insertSql, ‘,‘)); \
M("{$table}")->update();
}
原文地址:https://www.cnblogs.com/songyanan/p/10622433.html