Data Base Driven Website 4th edition - Chapter 4

Could use some help with this.

The previous example from this book in which I both output entries from a joke database and can add them in worked flawlessly. However when I added in this code:

if (isset($_GET[‘deletejoke’]))
{
$id = mysqli_real_escape_string($link, $_POST[‘id’]);
$sql = “DELETE FROM joke WHERE id=‘$id’”;
if (!mysqli_query($link, $sql))
{
$error = 'Error deleting joke: ’ . mysqli_error($link);
include ‘error.html.php’;
exit();
}

header('Location: .');
exit();

}

as well as update the while loop to store two columns in an array:

$jokes = array(‘id’ => $row[‘id’], ‘text’ => $row[‘joketext’]);

The index.php file doesn’t open in any browser. I’ve gone through all my files about 5 times, and there are no syntax errors, so barring any other issues, my only conclusion is that the author’s code must be incorrect. Has anyone had any issues with this, or know what the problem might be?

header(‘Location: .’);

change that to use an actual location like index.php
also pls use the php tags when posting code :slight_smile:

There are some differences though between which PHP tags are used.

The

 tag seems to be better when <?php is used


```php
 with &lt;?php

```php

<?php
$message = 'Hello world!';
?>
<p><?php echo $message;?></p>

 without <?php

```php

$message = 'Hello world!';
?&gt;
&lt;p&gt;&lt;?php echo $message;?&gt;&lt;/p&gt;

Whereas the

 tag seems to be more capable of color-coding  regardless of whether &lt;?php is used or not.


```php
 with <?php

```php

&lt;?php
$message = 'Hello world!';
?&gt;
&lt;p&gt;&lt;?php echo $message;?&gt;&lt;/p&gt;

 without &lt;?php

```php

$message = 'Hello world!';
?>
<p><?php echo $message;?></p>

@hash:

Ok, so I put the url of the index.php file in the header statement, although he recommends using the period to reload the same directory.

@pmw:
Thanks for the quick tutorial on php formatting for the forum.

If it helps ( and I know this may turn out long), here’s the entire code for my controller script:


if (isset($_GET['addjoke']))
{
	include 'form.html.php';
	exit();
}

$link = mysqli_connect('localhost', 'root', 'crust19');
if (!$link)
{
	$error = 'Unable to connect to the database server.';
	include 'error.html.php';
	exit();
}

if (!mysqli_set_charset($link, 'utf8'))
{
	$output = 'Unable to set database connection encoding.';
	include 'output.html.php';
	exit();
}

if (!mysqli_select_db($link, 'ijdb'))
{
	$error = 'Unable to locate the joke database.';
	include 'error.html.php';
	exit();
}

if (isset($_POST['joketext']))
{
	$joketext = mysqli_real_escape_string($link, $_POST['joketext']);
	$sql = 'INSERT INTO joke SET
			joketext="' . $joketext . '",
			jokedate=CURDATE()';
	if (!mysqli_query($link, $sql))
	{
		$error = 'Error adding submitted joke: ' . mysqli_error($link);
		include 'error.html.php';
		exit();
	}
	
	header('Location: /deletejoke/index.php');
	exit();
}

if (isset($_GET['deletejoke']))
{
	$id = mysqli_real_escape_string($link, $_POST['id']);
	$sql = "DELETE FROM joke WHERE id='$id'";
	if (!mysqli_query($link, $sql))
	{
		$error = 'Error deleting joke: ' . mysqli_error($link);
		include 'error.html.php';
		exit();
	}
	
	header('Location: /deletejoke/index.php');
	exit();
}
	
$result = mysqli_query($link, 'SELECT id, joketext FROM joke');
if (!$result)
{
	$error = 'Error fetching jokes: ' . mysqli_error($link);
	include 'error.html.php';
	exit();
}

while ($row = mysqli_fetch_array($result))
{
	$jokes[] = array('id' => $row['id'], 'text' => $row['joketext']);
}

include 'jokes.html.php';	

Have you enabled error checking for the code?


error_reporting(E_ALL);

Anything in the Apache error logs? The reason I ask is that a few people have had Apache crash because of that script.

I had a look in the error log, and I see multiple entries for an error telling me favicon.ico doesn’t exist

After some selective code commenting, the template page displays, and I have the problem narrowed down to the sql select statement in the script, which is



$result = mysqli_query($link, 'SELECT id, joketext FROM joke');
if (!$result)
{
	$error = 'Error fetching jokes: ' . mysqli_error($link);
	include 'error.html.php';
	exit();
}

I found that if I took out the id, and just kept the SELECT statement fetching the joketext element from the database, the template displays, albeit not correctly, but its a step in the right direction.

Hi

Have you tried


$result = mysqli_query($link, 'SELECT joke.id, joke.joketext FROM joke');


I have had errors in the past which have been a result of not listing the field in full. Have you also tried SELECT * which would bring everything and you can pick out what you want?

Hope it helps

Keith

Hi Keith,

I tried both methods, but unfortunately neither worked. I have a feeling that it’s a combination of selecting both elements from the database and the author’s while loop:


while ($row = mysqli_fetch_array($result))
{
	
	$jokes[] = array('id' => $row['id'], 'text' => $row['joketext']);
}

@swbates:
What happens if you have submitted a form with some field = ‘joketext’ & action = ‘index.php?deletejoke=yes’?