Jump to content

mysqli_query error occuring


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

if (!$result)
{
	$error = 'Error getting list of items to delete.';
	include 'error.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.';
		include 'error.html.php';
		exit();
	}

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

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

	 }
	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';
?>

 

 

The part of the above code that causes a problem is as follows:

 


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

 

Instead of deleting the category as the code requests, it's outputting the following error in the browser:

 

Error deleting category

 

So when it checks if the mysqli_query equates to false, ie:

 

if (!mysqli_query($link, $sql))

 

...it's outputting the value of $error.

 

I can't work out why mysqli_query is evaluating to false.

 

Can someone see some glaring error in the controller code that I may have missed? Or if further code from the included files is needed, let me know and I can post this also.

 

Really appreciate any help.

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/188873-mysqli_query-error-occuring/
Share on other sites

chances are the query is failing due to data you think is there but isn't. add a line in the failure control code block to output the query sent to the server, and the error that mysql has returned:

if (!mysqli_query($link, $sql))
{
   print('Query: '.$sql.'<br/>Error given: '.mysqli_error().'<br/>'."\n");
   $error = 'Error deleting category.';
   include 'error.html.php';
   exit();
}

Ok great - that code returned the error from mysql. I just needed to change id to catID so it deletes successfully now.

 

Only problem is that I'm getting the following error:

 

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/includes/output.html.php:10) in /Applications/MAMP/htdocs/admin/catalogue/categories/index.php on line 49

 

Line 49 is:

 

header('Location: .');

 

So since this lines tells the browser to reload the controller page using the URL of the parent directory, I don't know why it's not loading.

 

Any ideas appreciated.

 

headers already sent by (output started at /Applications/MAMP/htdocs/includes/output.html.php:10

 

check line 10 to see what is being output. i have never played around with headers so dont know exactly what the problem is. but it says "headers already sent" and you are trying to send them again.

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.