Hi everyone, The code below is the code in a template file that lists categories from a database: <?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>Manage Categories</h1> <p><a href="?add">Add new category</a></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="action" value="Edit"/> <input type="submit" name="action" value="Delete"/> </div> </form> </li> <?php endforeach; ?> </ul> <p><a href="..">Return to apparel management system home</a></p> </body> </html> Code (markup): It's controlled by code in a controller file (see below). In the above code the catID is stored in a hidden field so that when the delete submit button is pressed I can access it via the $_POST[ ] array in the controller file. The code in the controller file checks to see if the submit button that has a value of 'Delete' is pressed and if so it stores $_POST['catID'] in the $id variable. As I need to check whether the requested category to be deleted is already in use in the items table, I check this via a mysqli query, ie. SELECT itemID FROM items WHERE catID='$id'" I then check whether any records were found: if (mysqli_num_rows($result) > 0) After that point in the code I need to store the value of the category that was clicked on (in the template file above) in a variable so that I can use it in another file. This other file (category_delete.html.php) does two things - 1) if a match was made in the above SQL, it outputs a message saying that the category has dependent records and can't be deleted, and 2) if there were no dependent records then the category is deleted. So my problem is I'm not sure how I can store the value of the category that was clicked on in the above template file in a variable. My first thought was to create another SQL query that does the following: $sql = "SELECT category FROM categories WHERE catID='$id'"; I've tried this in the controller code below but I wasn't sure if this was the right way to go about it, since the category already exists in the above template file but as it's not stored in an input field I can't access it via the _POST[ ] array. It's just stored within two div tags in the form, ie. <div> <?php htmlout($category['category']); ?> .... </div> So I wondered if someone could comment on this and advise whether I do need to actually create this extra query or whether there's some other way to access the category from the above template form and then store it in the controller file so it's accessible in the category_delete.html.php file. If the query is needed it's throwing up an error but I'll wait for some initial comments before posting this. Appreciate any help. <?php if (isset($_POST['action']) and $_POST['action'] == 'Delete') { include $_SERVER['DOCUMENT_ROOT'] . '/includes/dbAdmin.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 (mysqli_num_rows($result) > 0) { $recordsExist = true; include $_SERVER['DOCUMENT_ROOT'] . '/includes/dbAdmin.inc.php'; $sql = "SELECT category FROM categories WHERE catID='$id'"; $result = mysqli_query($link, $sql); if (!$result) { $error = 'Error getting category.'; include 'error.html.php'; exit(); } while ($row = mysqli_fetch_array($result)) { $categories[] = $row['category']; } 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'; ?> Code (markup):
Thanks for the reply, I haven't used $_SESSIONs yet but it looks like the way to go. So the following code (in my controller file), contains the query that selects the category: // 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'; ?> Code (markup): To capture the category value in a session, do I put the following code at the top of the controller file: session_start(); $_SESSION['category'] = $category; I'm not sure if this is correct? Maybe it should be: session_start(); $_SESSION['category'] = $row['category']; Or should I store the session in a variable so I can access it on another page. As I haven't used sessions before I just need a bit more guidance.
$_SESSION variables are like $_GLOBAL but it loses value once user closes browser, also it is unique for every visitor, they reside in a hash. You only need to do session_start(); once, so make sure it's the first thing executed, make sure it's not executed twice in includes etc. To prevent it from being changed. if (!isset($_SESSION['category']) || empty($_SESSION['category'])) { $_SESSION['category'] = $row['category']; } PHP:
Ok so my controller code now looks like this: <?php if (isset($_POST['action']) and $_POST['action'] == 'Delete') { include $_SERVER['DOCUMENT_ROOT'] . '/includes/dbAdmin.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 (mysqli_num_rows($result) > 0) { $recordsExist = true; include 'category_delete.html.php'; session_start(); if (!isset($_SESSION['category']) || empty($_SESSION['category'])) { $_SESSION['category'] = $row['category']; } } 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'; ?> Code (markup): I'm not sure I inserted the $_Session code in the correct spot but when I test by loading the controller, the list of categories appear correctly, then I press the delete button and it goes to the category_delete.html.php page but it throws up an errors as follows: Notice: Undefined variable: categories in /Applications/MAMP/htdocs/admin/catalogue/categories/category_delete.html.php on line 16 Line 16 is: echo '<p class="warning">'. htmlout($category['category']) . ' category has dependent records. Can\'t be deleted.</p>'; The full code for category_delete.html.php is below: <?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']) . ' 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> <p><a href="?add">Add new category</a></p> <ul> <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> </ul><?php } ?> <p><a href="..">Return to apparel management system home</a></p> </body> </html> Code (markup): Any idea what I'm doing wrong?
Try a print_r($category); Then try a print_r($_SESSION); print_r(); is useful for debugging. If nothing appears for $_SESSION, you need a session_start() at the top.
I wasn't sure where to put print_r($category) or print_r($_SESSION) so I tried moving session_start() to the top of the controller page and then moved the following code further down in the code so that it executes before the list of categories are displayed on categories.html.php if (!isset($_SESSION['category']) || empty($_SESSION['category'])) { $_SESSION['category'] = $row['category']; $category = $_SESSION['category']; } Code (markup): I also stored the session in a variable called $category and then output this variable on the category_delete.html.php page, ie. echo '<p class="warning">'. $category . ' category has dependent records. Can\'t be deleted.</p>'; Code (markup): I'm still getting the same error though. The following is the full controller code at this point: <?php session_start(); ?> <?php if (isset($_POST['action']) and $_POST['action'] == 'Delete') { include $_SERVER['DOCUMENT_ROOT'] . '/includes/dbAdmin.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 (mysqli_num_rows($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']); } if (!isset($_SESSION['category']) || empty($_SESSION['category'])) { $_SESSION['category'] = $row['category']; $category = $_SESSION['category']; } include 'categories.html.php'; ?> Code (markup): Any further ideas?
When I do that it outputs: Array category has dependent records. Can't be deleted. So instead of the category name it's outputting Array.
I'm not sure where to put print_r($category) or print_r($_SESSION). Can you tell me where in the code to put this? I'm just starting out with php so I'm still learning.
Ok so I put print_r($_SESSION) and I got the following output by the browser: Array ( [category] => Array ( [catID] => 3 [category] => Shirts ) ) I don't know why it's outputting category 3 which is the shirts category when I clicked on a completely different category to be deleted. Any thoughts?