Hi everyone,
I have the following code in a controller file:
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/magicquotes.inc.php';
if (isset($_GET['add']))
{
$pagetitle = 'New Category';
$action = 'addform';
$name = '';
$email = '';
$catID = '';
$button = 'Add category';
include 'form.html.php';
exit();
}
if (isset($_GET['addform']))
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/dbAdmin.inc.php';
$category = mysqli_real_escape_string($link, $_POST['category']);
$sql = "INSERT INTO categories SET
category='$category'";
if (!mysqli_query($link, $sql))
{
$error = 'Error adding submitted category.';
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
if (isset($_POST['action']) and $_POST['action'] == 'Edit')
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/dbAdmin.inc.php';
$id = mysqli_real_escape_string($link, $_POST['catID']);
$sql = "SELECT catID, category FROM categories WHERE catID='$id'";
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error fetching category details.';
include 'error.html.php';
exit();
}
$row = mysqli_fetch_array($result);
$pagetitle = 'Edit Category';
$action = 'editform';
$category = $row['category'];
$catID = $row['catID'];
$button = 'Update category';
include 'form.html.php';
exit();
}
if (isset($_GET['editform']))
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/dbAdmin.inc.php';
$catID = mysqli_real_escape_string($link, $_POST['catID']);
$category = mysqli_real_escape_string($link, $_POST['category']);
$sql = "UPDATE categories SET
category='$category'
WHERE catID='$catID'";
if (!mysqli_query($link, $sql))
{
$error = 'Error updating submitted category.';
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
The above code works in tandem with the following template file:
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>
<?php htmlout($pagetitle); ?>
</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8"/>
</head>
<body>
<h1><?php htmlout($pagetitle); ?></h1>
<form action="?<?php htmlout($action); ?>" method="post">
<div>
<label for="name">Category: <input type="text" name="category"
id="category" value="<?php htmlout($category); ?>"/></label>
</div>
<div>
<input type="hidden" name="id" value="<?php
htmlout($catID); ?>"/>
<input type="submit" value="<?php htmlout($button); ?>"/>
</div>
</form>
</body>
</html>
When editing a category by clicking the submit button in the above file, I get the following error:
Notice: Undefined index: catID in /Applications/MAMP/htdocs/admin/catalogue/categories/index.php on line 65
Line 65 in the controller file is:
$catID = mysqli_real_escape_string($link, $_POST['catID']);
Can anyone see why this error is occurring?
Appreciate any help.