Build your own Database Driven Site

Hi,

I recently finished chapter 7 in the book (which resulted in a working content management system), editing all of the categories jokes and authors was working perfectly. So I preceded to Chapter 8, after reaching the end of Chapter 8 I went to edit some jokes to contain BBCode tags to check everything was working fine, after clicking the edit button, I was presented with the following error:

"Notice: Undefined variable: selectedCategories in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\admin\jokes\index.php on line 169

Warning: in_array() expects parameter 2 to be array, null given in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\admin\jokes\index.php on line 169"

I have been through the code and even copied the whole code for the file from the code archive, and still the same error. I also notice that where the author, category and joke text used to auto populate it doesn’t any more. I can’t seem to figure this one out.

Any ideas would be much appreciated.

Many Thanks

If we’re on the same page the trouble spot is

	while ($row = mysqli_fetch_array($result))
	{
		$categories[] = array(
				'id' => $row['id'],
				'name' => $row['name'],
				'selected' => in_array($row['id'], $selectedCategories));
	}

It doesn’t look like the array is being initialized here

	while ($row = mysqli_fetch_array($result))
	{
		$selectedCategories[] = $row['categoryid'];
	}

You could try adding a line like so

$selectedCategories = array();
	while ($row = mysqli_fetch_array($result))
	{
		$selectedCategories[] = $row['categoryid'];
	}

That would prevent the error messages, but I suspect there’s a problem elsewhere and the array will be empty.

Try

	// Get list of categories containing this joke
	$sql = "SELECT categoryid FROM jokecategory WHERE jokeid='$id'";
	$result = mysqli_query($link, $sql);
	if (!$result)
	{
		$error = 'Error fetching list of selected categories.';
		include 'error.html.php';
		exit();
	}
	else
	{
		var_dump($result);
	}

u need to paste whats on line 169 in order to us to help u :slight_smile:

I am having the same problem as described above with the $selectedCategories variable being null:

Notice: Undefined variable: selectedCategories in /Applications/MAMP/htdocs/chapter7/admin/jokes/index.php on line 169

Warning: in_array() expects parameter 2 to be array, null given in /Applications/MAMP/htdocs/chapter7/admin/jokes/index.php on line 169

But the proposed fixes don’t seem to be working for me.When editing a joke, if a category, any category has already selected, there is no error message. If a joke has no assigned category, I get the error, but also a working editform where I can assign a category and go on with life. However, I prefer not to receive error messages.

I downloaded the script that comes with the Build your own database… to see if it would work, but I get exactly the same message. I am surprized that not more people have reported this, since it seems to be a problem with the script provided in the book - maybe people always had a category checked. Anyway, I would be really grateful if somebody could help me on this. My code is exactly as mentioned above. Thanks.