keep getting this error: Undefined index: id in C:\xampp\htdocs\Projeto\edit.php on line 5
But i checked phpmyadmin and i have a index for id, it's in the table. Why do I get this? Never happened before.

<?php

require 'conf/connection.php';

$id = $_GET['id'];

$choose_user = mysql_query("SELECT * FROM users WHERE id = '$id'");
$r = mysql_fetch_array($choose_user);

?>

Dani AI

Generated

As noted, the PHP notice "Undefined index: id" simply means the script tried to read an array key that wasn't sent (no id in the query string). On XAMPP that often happens when the page is opened directly (for example, visiting edit.php without ?id=...) or when the form/link uses a different request method. A more robust approach validates the input and avoids the old mysql_* API.

A safer pattern: validate the incoming id as an integer, handle the missing/invalid case explicitly, and use prepared statements (PDO/mysqli) so SQL injection is prevented and the code works on modern PHP versions. For example:

<?php
// get id as an integer from the query string
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if ($id === null || $id === false) {
    http_response_code(400);
    echo 'Missing or invalid id.';
    exit;
}

$pdo = new PDO('mysql:host=localhost;dbname=Projeto;charset=utf8mb4', 'dbuser', 'dbpass', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id');
$stmt->execute(['id' => $id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$user) {
    http_response_code(404);
    echo 'Record not found.';
    exit;
}

Other practical notes: confirm the link or form actually sends id (test with a URL like http://localhost/Projeto/edit.php?id=1), prefer explicit GET/POST checks over $_REQUEST, enable E_ALL error reporting while developing, and avoid leaving detailed errors visible in production. The quick isset-style fix resolves the notice, but input validation and prepared statements make the code safer and future-proof.

Recommended Answers

All 2 Replies

Member Avatar for Member #120589

It's the fact that there is no url querystring.

Use this:

<?php
if(isset($_GET['id'])){ 
  require 'conf/connection.php';
 
  $id = $_GET['id'];
 
  $choose_user = mysql_query("SELECT * FROM users WHERE id = '$id'");
  $r = mysql_fetch_array($choose_user);
}
?>

You also need to clean your input - have a look at mysql_real_escape_string() in the php.net manual.

Problem solved! Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.