在model.php
中,我有一个方法可以用于更多的表,因此我将参数传递给表的名称。我的目的是使用'code' = $code
function edit_parameter($description, $code, $table){
$param = array(
'description' => $description
);
$this->db->where('code', $code);
$this->db->update($table, $param);
}
当我运行此代码时,我获得500 error
,但如果我放入
$this->db->update($table, $param);
表的名称,它的代码。有人可以帮帮我吗?
答案 0 :(得分:1)
希望这会对您有所帮助:
对于500错误检查你调用正确的控制器方法,然后调用edit_parameter
方法
除非$table
为空,否则它应该有效;
首先检查$table
是否empty
function edit_parameter($description,$code,$table)
{
if (!empty($table))
{
$param=array('description'=>$description);
$this->db->where('code', $code);
if (! $this->db->update($table,$param)) {
return $this->db->error();
}
}
else
{
$status = 'table not found !';
return $status;
}
}
答案 1 :(得分:0)
试试这个,我希望它有效
function edit_parameter($description, $code, $table = null) {
if ($table == null || !is_string($table)) return;
$param = array(
'description' => $description
);
$this->db->where('code', $code);
if(!$this->db->update($table, $param)) {
return $this->db->error();
}
}
答案 2 :(得分:0)
虽然其他答案可能会起作用,但这不是你想要的。
function edit_parameter($description, $code, str $table) {
$param = array(
'description' => $description
);
$this->db->where('code', $code);
if(!$this->db->update($table, $param)) {
return $this->db->error();
}
}
这样你就强制执行$ table,所以你甚至无法在不填充$ table的情况下进入函数。您不应该允许$ table为空,然后运行检查以查看它是否为空,因为没有它,您的函数将无法工作。因此,为什么你应该强迫它被填补。