Php 未定义变量:id(GET)

Php 未定义变量:id(GET),php,Php,Undefined variable:id,我通过每次选择它来测试id,它工作正常,所以它是如何未定义的 霉菌控制者 if(isset($_GET['deleteid'])) { $id = $_GET['deleteid']; require_once '../model/dsections.php'; $delete = new Dsections("sections"); $delete->deletesec

Undefined variable:id,我通过每次选择它来测试id,它工作正常,所以它是如何未定义的

霉菌控制者

 if(isset($_GET['deleteid']))
    {
        $id  = $_GET['deleteid'];
        require_once '../model/dsections.php';
        $delete = new Dsections("sections");
        $delete->deletesec($id);
        require_once '../view/vsections.php';



}
模型

} mymodel中此行的错误
$this->deletesec($id)

在您的
dssection
构造函数中调用
$this->deletesec($id)在未定义的$id上:)

没有删除函数,因为您稍后仍要调用它

您只需向构造函数添加
$id
,如下所示:

  public function __construct($tablename,$id)
  {
      //DB table name.
      $this->tablename = $tablename;
      //DB connection.
      $this->connectTodb();
      //Delete function
      $this->deletesec($id);
  }

但是不建议这样做,因为它违反了最重要的软件工程原则(IMHO),即首先尝试转换/转换您的
$id=(int)$\u GET['deleteid'];/*或者*/$id=intval($_GET['deleteid'];
为整数,或者在SQL查询中将其括在单引号中


注意:您可以在您的
deletesec
签名中使用类型暗示来始终获得真实值,但在PHP<7中不能使用标量类型。

您可以发布model/dsections.PHP吗?还有一件事,您在
require_once'../model/dsections.PHP';
之前和之后都尝试回显
$id
了吗在删除$this->deletesec($id)之前和之后;在构造函数中请检查我的答案当我删除$id时,它会给我2个错误缺少dssections::deletesec()的参数1,以及未定义的变量:db queryYou是正确的,但他试图直接调用它,而不是从构造函数中调用。
$delete=new dssections(“sections”);$delete->deletesec($id)
不要删除$id…你必须删除整行,因为你在主文件中调用它!是的,我得到了它,错误消失了,但我得到了错误处理请求,它来自异常,我很抱歉,我是一个新的OOP。如果它解决了你的问题,你能请+1并接受答案吗
  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();
  }
  public function __construct($tablename,$id)
  {
      //DB table name.
      $this->tablename = $tablename;
      //DB connection.
      $this->connectTodb();
      //Delete function
      $this->deletesec($id);
  }