-3

Undefined variable: id, i tested the id by eachoing it and it worked so how it's Undefined

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';



}

model

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");

    }
}

} the error at this line in mymodel $this->deletesec($id);

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
ziko310
  • 1
  • 4

2 Answers2

1

in your Dsection constructor you call $this->deletesec($id); on $id which is undefined :)

  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

  }

The constructor must look like:

  public function __construct($tablename)
  {

      //DB table name.
      $this->tablename = $tablename;
      //DB connection.
      $this->connectTodb();
  }

Without delete function because you are calling it anyway later!

OR you can simply add the $id to the constructor like this:

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

BUT this is not recommended, because it violates the most important software engineering principle (IMHO) which is the Single Responsibility Principle

Ahmad Hajjar
  • 1,796
  • 3
  • 18
  • 33
  • when i remove $id it gives me 2 errors Missing argument 1 for Dsections::deletesec(), and Undefined variable: id in db query – ziko310 Aug 19 '16 at 03:59
  • You are correct but he is trying to call it directly not from the constructor. `$delete = new Dsections("sections"); $delete->deletesec($id);` – MNR Aug 19 '16 at 04:00
  • 1
    dont remove $id ... you must remove the whole line, since you are calling it in the main file ! – Ahmad Hajjar Aug 19 '16 at 04:00
  • yeah i got it and th error disappear but i got Error Processing Request which from the exception and i'm sorry i'm a new oop – ziko310 Aug 19 '16 at 04:09
  • Can you please +1 and accept the answer if it solved your problem – Ahmad Hajjar Aug 19 '16 at 04:11
0

First try to either convert/cast your $id = (int) $_GET['deleteid']; /* or */ $id = intval($_GET['deleteid']; to an integer or surround it in single quotes '$id' in your SQL query.

NOTE: You can use type-hinting in your deletesec signature to always get a true value but not for scalar types in PHP < 7.

MNR
  • 727
  • 1
  • 9
  • 23