Debugging "Undefined index: id" in Legacy PHP Forms
The error Notice: Undefined index: id on line 21 of your script is a classic symptom of a logic flow mismatch between how data is sent to a page and how that page expects to receive it. In your specific case, the issue isn't strictly about the database query failing; it's about the variable $id being accessed in the HTML output before you have confirmed it actually exists in the $_GET superglobal.
Looking at your code snippet, the error occurs because the line $row=mysql_fetch_array($query); likely returned an empty array or failed due to a missing row, yet the script continues executing. However, the specific notice mentioned usually points to a different location: where you attempt to echo $row['itemname'] or $id inside the <form> tag.
The root cause in your provided code is actually a subtle timing issue combined with potential SQL logic errors. You are retrieving the ID via $_GET, but if the user accesses change.php without appending ?id=5 to the URL, the index 'id' simply does not exist in the $_GET array. When PHP tries to access $_GET['id'] on line 5 without checking its existence first, it triggers a warning or notice depending on your error reporting settings (error_reporting).
Furthermore, there is a critical logical flaw in how you handle the database result. If the query select * from manuf where id='$id' returns no rows (perhaps the ID was wrong, or the table is empty), mysql_fetch_array() will return false. In older PHP versions, accessing properties on false might behave differently, but in modern contexts, you must ensure $row is an array before trying to access $row['itemname'].
Here is a robust way to restructure this logic. First, always verify that the ID parameter exists and is a valid integer before using it in a query:
if (!isset($_GET['id'])) {
die("No item ID provided.");
}
$id = (int)$_GET['id']; // Cast to int to prevent SQL injection
$query = "SELECT * FROM manuf WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
die("Item not found.");
}
$row = $result->fetch_assoc();
This approach eliminates the "Undefined index" error immediately by gating the logic behind an isset check. It also casts the input to an integer, which is a fundamental security requirement when dealing with IDs.
A secondary issue in your original code involves the deprecated mysql_* functions. The mysql extension has been removed from PHP core for years. Using it means you are running on an outdated stack that lacks prepared statements and modern error handling. Switching to PDO or MySQLi is not just about fixing notices; it's about security. Your current query concatenates the user input directly into the SQL string (WHERE id='$id'). If a malicious user manipulates the ID parameter, they could bypass the filter entirely.
Regarding the form submission to updateprice.php, ensure that updateprice.php handles the incoming POST data safely as well. Since you are using $_GET['id'] to populate the hidden input field in your form, and then presumably sending that same ID back in a POST request, you must verify that the receiving script (updateprice.php) expects an ID in the POST array or retrieves it from the session/database directly if the workflow changes.
Finally, consider the trade-offs of using legacy syntax. While mysql_fetch_array() returns an associative array, mixing this with modern frameworks like Laravel (often associated with LaravelCompany) requires a complete rewrite to use Eloquent models and migrations. The pattern of "fetch then echo" is valid for simple scripts, but it becomes brittle quickly. By validating the existence of variables before accessing them and using prepared statements, you remove the ambiguity that causes these notices and make your application resilient against input errors.