Jump to content

Primary key is being output instead of value of variable


gwh

Recommended Posts

Hi everyone,

 

I have the following code in a controller file:

 

<?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'";
$item_result = mysqli_query($link, $sql);

if (!$item_result)
{
	$error = 'Error getting list of items to delete: ' . mysqli_error($link);
	include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php';
	exit();
}

$sql = "SELECT category FROM categories WHERE catID='$id'";
$category_result = mysqli_query($link, $sql);

if (!$category_result)
{
	$error = 'Error getting category to display: ' . mysqli_error($link);
	include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php';
	exit();
}


// assume that no match has been found
	$recordsExist = false;


// check whether recordset found any matches
if (mysqli_num_rows($item_result) > 0) {

	$recordsExist = true;

	list($category) = mysqli_fetch_row($item_result);

 	include 'category_delete.html.php';
	exit();

	 	}
		elseif (mysqli_num_rows($item_result) == 0) {

		list($category) = mysqli_fetch_row($category_result);

		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();

}

?>

 

When the following code block (from the above controller file) is executed it includes a file called category_delete.html.php:

 

// check whether recordset found any matches
if (mysqli_num_rows($item_result) > 0) {

	$recordsExist = true;

	list($category) = mysqli_fetch_row($item_result);

 	include 'category_delete.html.php';
	exit();

 

In the above code block, the results from the $item_result array is extracted into the variable $category via the list function and it's this variable value that needs to be inserted in the included category_delete.html.php page.

 

The following are the contents of category_delete.html.php:

 

<?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>

 

The following line in the above file is supposed to output the value of the variable called $category:

 

echo '<p class="warning">'. $category . ' category has dependent records. Can\'t be deleted.</p>';

 

Instead of outputting the value however, it's outputting the primary key, eg.

 

1 category has dependent records. Can't be deleted

 

Does anyone know why this is happening?

 

Appreciate any help.

 

 

 

 

	
// check whether recordset found any matches
if (mysqli_num_rows($item_result) > 0) {
$recordsExist = true;
list($category) = mysqli_fetch_row($item_result); 
include 'category_delete.html.php';
exit();

 

Don't you think "list($category) = mysqli_fetch_row($item_result); " this line should say "list($category) = mysqli_fetch_row($category_result); "?

 

Regards,

Anees

Amoeba Solution Kiosk

http://ask.amoeba.co.in

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.