Php 如何在更新代码点火器中放入类似名称表的变量
在Php 如何在更新代码点火器中放入类似名称表的变量,php,codeigniter,codeigniter-3,Php,Codeigniter,Codeigniter 3,在model.php中,我有一个用于更多表的方法,因此我像参数一样传递表的名称。我的目的是用'code'=$code function edit_parameter($description, $code, $table){ $param = array( 'description' => $description ); $this->db->where('code', $code); $
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错误
,但如果我输入
$this->db->update($table, $param);
表的名称及其工作的代码。有人能帮我吗?希望这对你有帮助: 对于500错误,请检查您调用正确控制器的方法,该方法反过来调用
edit\u参数
方法
除非$table
为空,否则它应该工作
首先检查$table
是否为非空
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;
}
}
试试这个,我希望它能起作用
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();
}
}
虽然其他答案可能会起作用,但这不是你想要的
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为空,然后运行检查以查看它是否为空,因为没有它,您的函数将无法工作。因此,您应该强制填充它。当然,如果您运行代码而不传递表名,它会失败,为什么不呢?您还应该真正开发错误报告,而不是猜测问题。$table是table的名称!“但是如果我输入表的名称,代码就会工作”。。。因此,如果不输入表名,则会发生500错误。如果不是这样,请修改您的问题,使其更清楚。更新显然要求$table是数据库中存在的表名。我的意思是,如果我传递像$table这样的变量,代码就不起作用,而如果我传递$this->db->update(“name_example”,$param);由于代码超出了您提供的代码范围,因此它工作良好。因为函数param中的$table与
$this->db->update($table,$param)之间的关系代码>是明确的,不是问题所在。如果我是你,我会调查为什么表param没有传递给函数,因为这是唯一有效的可能性。在我看来,此函数中不应包含此逻辑:如果控制器方法调用此函数时没有$table名称,则应通过在控制器中提供错误而在控制器中进行不同的处理。模型中的DB函数在被调用时应该具有它们工作所需的所有信息。这就是为什么我们经常看到验证发生在控制器中。再说一遍,我的意见。我同意,但似乎OP本身并不熟悉存储库,我并没有真的为此烦恼。