gwh Posted January 26, 2010 Share Posted January 26, 2010 Hi everyone, The following template page displays a list of categories in 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> The above code has an edit and delete submit button which when clicked loads a php controller page as follows: <?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: ' . mysqli_error($link); include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php'; exit(); } // check whether recordset found any matches if (mysqli_num_rows($result) > 0) { $recordsExist = true; $sql = "SELECT category FROM categories WHERE catID='$id'"; $result = mysqli_query($link, $sql); if (!$result) { $error = 'Error getting category to display: ' . mysqli_error($link); include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php'; exit(); } list($category) = mysqli_fetch_row($result); include 'category_delete.html.php'; exit(); } else { include 'category_delete.html.php'; exit(); // Delete the category $sql = "DELETE FROM categories WHERE catID='$id'"; if (!mysqli_query($link, $sql)) { $error = 'Error deleting category: ' . mysqli_error($link); include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.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! – ' . mysqli_error($link); include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php'; exit(); } while ($row = mysqli_fetch_array($result)) { $categories[] = array('catID' => $row['catID'], 'category' => $row['category']); } include 'categories.html.php'; ?> Code in the above controller file checks to see whether the category associated with the delete button that was clicked on, already exists in an items table and if it has it includes the category_delete.html.php page to display a message saying that the category could not be deleted. The following code is what constitutes this category_delete.html.php page: <?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">'. $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> <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 } ?> </body> </html> If there is a category already in use then the code functions as it should and the delete page with the message displays correctly. The goal of the code in the controller is that if the category is available for deletion then the code again includes the category_delete.html.php page so that the user can confirm the deletion before it takes place. Currently, it includes the file but the form that displays the category isn't being shown and it's also displaying the following Undefined variable errors: Notice: Undefined variable: recordsExist in /Applications/MAMP/htdocs/admin/catalogue/categories/category_delete.html.php on line 15 and Notice: Undefined variable: category in /Applications/MAMP/htdocs/admin/catalogue/categories/category_delete.html.php on line 28 Line 15 is: if ($recordsExist) { and Line 28 is: <?php htmlout($category['category']); ?> Does anyone know what I'm doing wrong? Appreciate any help. Link to comment https://forums.phpfreaks.com/topic/189825-undefined-variable-errors/ Share on other sites More sharing options...
jl5501 Posted January 26, 2010 Share Posted January 26, 2010 Taking the $recordsExist variable as an example, I can see your code that checks it, but cannot see anything that sets it. You will get a PHP Notice error if you reference a variable that has not been set. So, the code if($recordsExist) will produce this error if the variable has not been set to anything. Notice errors are the lowest level of errors in PHP, and are often suppressed, but can show poor coding practice, as in this case where all variables should be set to something before any logic is applied to them. You can check if a variable is set, without raising an error, by using isset($myvariable) Hope this helps Link to comment https://forums.phpfreaks.com/topic/189825-undefined-variable-errors/#findComment-1001715 Share on other sites More sharing options...
gwh Posted January 26, 2010 Author Share Posted January 26, 2010 Thanks for the reply, The $recordsExist variable is set in the controller page, ie. $recordsExist = true; Not sure why it's not carrying over into the delete page? Link to comment https://forums.phpfreaks.com/topic/189825-undefined-variable-errors/#findComment-1001726 Share on other sites More sharing options...
gwh Posted January 26, 2010 Author Share Posted January 26, 2010 Ok so I needed to add the following code outside of the if statement: $recordsExist = false; This has gotten rid of the first error relating to this variable. In addition to this I added a nested if statement to the else part of the statement as follows: else if (mysqli_num_rows($result) == 0) { list($category) = mysqli_fetch_row($result); include 'category_delete.html.php'; exit(); This eliminated the second variable error relating to $category. When category_delete.html.php loads now all errors are gone but it's still not outputting the value of $category in the following line: <?php htmlout($category); ?> Below is the full code in the delete 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>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">'. $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> <li> <form action="" method="post"> <div> <?php htmlout($category); ?> <input type="hidden" name="catID" value="<?php echo $category['catID']; ?>"/> <input type="submit" name="delete" value="Confirm deletion"/> </div> </form> </li> </ul><?php } ?> </body> </html> Any further help appreciated. Link to comment https://forums.phpfreaks.com/topic/189825-undefined-variable-errors/#findComment-1001736 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.