AndrB AndrB - 6 months ago 12
MySQL Question

undefined index id.. PHP

I'm trying to delete some data from a table in a mysqli database, and I receive that error. I tried with isset, writing it directly without using a variable, nothing works. What am I doing wrong?

<?php

echo"hello";

$link = mysqli_connect("localhost", "root", "", "documents");

if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}


$ID = $_POST['id'];

$result = mysqli_query($link, "DELETE FROM document WHERE id='$ID'");


header("Location: connect-db.php");
?>

Answer

If you are fetching id from URL then use

$ID =isset( $_REQUEST['id']) ? $_REQUEST['id'] : 0; 

OR

$ID =isset( $_GET['id']) ? $_GET['id'] : 0;

If you are fetching id from POST variables then use

$ID =isset( $_POST['id']) ? $_POST['id'] : 0;