Thanks for the replies,
I’m using MyIsam tables and have decided not to use the built-in InnoDB tables that have support for relational integrity. I basically just need practice with my coding which is why I’ve decided to use php to handle the deletions for this particular application.
I came up with the following code in a controller file:
<?php
if (isset($_POST['action']) and $_POST['action'] == 'Delete')
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
$id = mysqli_real_escape_string($link, $_POST['catID']);
// Get items with certain category
$sql = "SELECT itemID FROM items WHERE catID='$id'";
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error getting list of items to delete.';
include 'error.html.php';
exit();
}
// assume that no match has been found
$recordsExist = false;
// check whether recordset found any matches
if ($result > 0) {
$recordsExist = true;
include 'category_delete.html.php';
}
else {
// Delete the category
$sql = "DELETE FROM categories WHERE id='$id'";
if (!mysqli_query($link, $sql))
{
$error = 'Error deleting category.';
include 'error.html.php';
exit();
}
}
header('Location: .');
exit();
}
// Display category list
include $_SERVER['DOCUMENT_ROOT'] . '/includes/dbAdmin.inc.php';
$result = mysqli_query($link, 'SELECT catID, category FROM categories');
if (!$result)
{
$error = 'Error fetching categories from database!';
include 'error.inc.html.php';
exit();
}
while ($row = mysqli_fetch_array($result))
{
$categories[] = array('catID' => $row['catID'], 'category' => $row['category']);
}
include 'categories.html.php';
?>
It displays the list of categories well however when I click one of the delete buttons, I get the following errors:
Notice: Object of class mysqli_result could not be converted to int in /Applications/MAMP/htdocs/new_site/admin/catalogue/categories/index.php on line 22
Parse error: syntax error, unexpected ';' in /Applications/MAMP/htdocs/new_site/admin/catalogue/categories/category_delete.html.php on line 16
index.php is the controller file I’ve shown above and line 22 is the following line:
if ($result > 0) {
The following is the category_delete.html.php template file that’s included after the code checks whether the recordset found any matches:
<?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>Manage Categories</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8"/>
</head>
<body>
<h1>Delete Category</h1>
<p>
<?php
if ($recordsExist) {
echo '<p class="warning">'. htmlout($category['category'] . ' has dependent records. Can\\'t be deleted.</p>';
}
else {
?>
</p>
<p class="warning">Please confirm that you want to delete the following record. This operation cannot be undone. </p>
<ul>
<?php foreach ($categories as $category): ?>
<li>
<form action="" method="post">
<div>
<?php htmlout($category['category']); ?>
<input type="hidden" name="catID" value="<?php
echo $category['catID']; ?>"/>
<input type="submit" name="delete" value="Confirm deletion"/>
</div>
</form>
</li>
<?php endforeach; ?>
</ul><?php } ?>
<p><a href="..">Return to category list</a></p>
</body>
</html>
Line 16 that’s mentioned in the error is as follows:
echo '<p class="warning">'. htmlout($category['category'] . ' has dependent records. Can\\'t be deleted.</p>';
I wondered if anyone knows what the errors are about and what I need to do to get this working?
Appreciate any advice.