未定义的变量:id,我通过peroing测试了id,并且它的工作原理是如何定义
myController的
if(isset($_GET['deleteid']))
{
$id = $_GET['deleteid'];
require_once '../model/dsections.php';
$delete = new Dsections("sections");
$delete->deletesec($id);
require_once '../view/vsections.php';
}
模型
class Dsections extends cone{
protected $tablename;
public function __construct($tablename)
{
//DB table name.
$this->tablename = $tablename;
//DB connection.
$this->connectTodb();
//Delete function
$this->deletesec($id);
}
public function deletesec($id)
{
// Delete a specific section.
$query = "DELETE * FROM $this->tablename WHERE id = $id ";
if (!$sqli = mysqli_query($this->cxn->connect(),$query)) {
throw new Exception("Error Processing Request");
}
}
} mymodel中此行的错误 $这 - > deletesec($ ID);
答案 0 :(得分:1)
在Dsection
构造函数中,您在$ id上调用了$this->deletesec($id);
,这是未定义的:)
public function __construct($tablename)
{
//DB table name.
$this->tablename = $tablename;
//DB connection.
$this->connectTodb();
//Delete function
$this->deletesec($id); // <--------- this line here $id is not defined here
}
构造函数必须如下所示:
public function __construct($tablename)
{
//DB table name.
$this->tablename = $tablename;
//DB connection.
$this->connectTodb();
}
没有删除功能,因为你以后要调用它!
OR 您只需将$id
添加到构造函数中,如下所示:
public function __construct($tablename,$id)
{
//DB table name.
$this->tablename = $tablename;
//DB connection.
$this->connectTodb();
//Delete function
$this->deletesec($id);
}
但这不推荐,因为它违反了最重要的软件工程原则(IMHO)Single Responsibility Principle
答案 1 :(得分:0)
首先尝试将$id = (int) $_GET['deleteid']; /* or */ $id = intval($_GET['deleteid'];
转换/转换为整数,或在SQL查询中以单引号'$id'
包围它。
注意:您可以在deletesec
签名中使用类型提示来始终获得真值,但不能使用PHP中的标量类型&lt; 7。