未定义变量:id,我测试了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中这一行的错误$ this -> deletesec ($ id);
在您的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();
}
没有delete函数,因为您稍后会调用它!
或您可以简单地将$id
添加到构造函数中,像这样:
public function __construct($tablename,$id)
{
//DB table name.
$this->tablename = $tablename;
//DB connection.
$this->connectTodb();
//Delete function
$this->deletesec($id);
}
但是不建议这样做,因为它违反了最重要的软件工程原则(IMHO),即单一责任原则
首先尝试将您的$id = (int) $_GET['deleteid']; /* or */ $id = intval($_GET['deleteid'];
转换为整数或在SQL查询中以单引号'$id'
包围它。
注意:你可以在你的deletesec
签名中使用类型提示来总是得到一个真值,但在PHP <7 .