1

I have the following button which should delete the item which has been chosen

$content .= "<td> " . "<a href=/index.php?p=3&id=$id>delete</a>";

However this always sends me to:

http://localhost/index.php?p=3&id=

When p=3 it should call the following function:

case 3:
$items = new dbconnection();
$content = $items->deleteItem($_GET['id']);

/

public $id;
public function deleteItems($id) {
    $conn = dbconnection::startconnection();

    $this->id = $id;

    $sql = "DELETE FROM items where id = '$this->id'";
    $stmt = $conn->prepare($sql);
    $stmt->execute();

    $content ="<p>" . "<a href=index.php>Go back</a>" . "</p>";

    return $content;
}
public function __construct($id = 1) {

}

Why is the ID not getting send to the URL?

RWRkeSBZ
  • 723
  • 4
  • 11
Djem
  • 19
  • 2
  • 2
    `var_dump($id)` and see if there is a value. Also: **Your code is vulnerable to SQL injection and will be hacked** even if [you are escaping inputs!](https://stackoverflow.com/a/5741264/2595450) Use [Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead. Check: [How can I prevent SQL injection in PHP](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Spoody Apr 27 '18 at 15:37
  • `$id` is not set or is empty. `error_reporting(E_ALL); ini_set('display_errors', '1');` – AbraCadaver Apr 27 '18 at 15:39
  • Either you haven't defined `$id`, it's empty or it's not within the scope of your function. Enable error reporting and dump it, should gives you clues where to look. We haven't seen enough code to say for sure. – Qirel Apr 27 '18 at 15:41
  • Shouldn't it be: $item->deleteItem($_GET['id']); Remove the "s" so it matches your dbconnection variable right before it? – dukedevil294 Apr 27 '18 at 15:42
  • I won't publish it so I understand that it could be vulnerable, I tried to vardump ID and it looks like it's NULL, could the problem be in the switch case where it only calls the function if the page is `p=3` does not have `&id=` ? – Djem Apr 27 '18 at 15:47
  • post more code of the page where the button is – the_nuts Apr 27 '18 at 16:07

0 Answers0