BYO Database Driven...Chapter 4 Add and deletejoke work, but list is not refreshed

Hello,

I worked through Chapter 4 and had problem with Adding jokes. Jokes would add to the database, but the joke list would not refresh. I resolved this by removing the exit(); after the header (‘Location: .’). I continued with delete joke and came up with the same problem, and resolved it in a similar fashion, but I fear I doing two wrongs to make it right. I tried the downloaded code and that did refreshed with the exit (), but I can’t seem to find the problem with my code - perhaps a typo. Anybody have any bright ideas? Hereunder, I have included the code with the exits. Thanks for your time.

Many thanks,

caboom

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Jokes</title>
</head>

<body>
 
<?php  
if (get_magic_quotes_gpc())  
{  
 function stripslashes_deep($value)  
 {  
   $value = is_array($value) ?  
       array_map('stripslashes_deep', $value) :  
       stripslashes($value);  
 
   return $value;  
 }  
 
 $_POST = array_map('stripslashes_deep', $_POST);  
 $_GET = array_map('stripslashes_deep', $_GET);  
 $_COOKIE = array_map('stripslashes_deep', $_COOKIE);  
 $_REQUEST = array_map('stripslashes_deep', $_REQUEST);  
}  
 
if (isset($_GET['addjoke']))  
{  
 include 'form.html.php';  
 exit();  
}  

$link = mysqli_connect('localhost', 'root', 'Fabiola');  
if (!$link)  
{  
 $error = 'Unable to connect to the database server.';  
 include 'error.html.php';  
 exit();  
}  
 
if (!mysqli_set_charset($link, 'utf8'))  
{  
 $error = 'Unable to set database connection encoding.';  
 include 'error.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: .');
  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: .');
 exit();
 
}  
 
$result = mysqli_query($link, 'SELECT id, joketext FROM joke');  
if (!$result)  
{  
 $error = 'Error fetching jokes: ' . mysqli_error($link);  
 include 'error.html.php';  
}  
 
while ($row = mysqli_fetch_array($result))  
{  
 $jokes[] = array('id' => $row['id'], 'text' => $row['joketext']);  
}  
 
include 'jokes.html.php'; 


?>

</body>
</html>

This appears to be the php processing page which doesnt need and on the html declarations. delete ALL the html tags to leave only the <?php ?> and make sure that the opening tag is right at the top.

I’m getting the same result at the add joke point in chapter 4, and yes, removing the exit() statement right after “header(‘Location: .’);” somehow enables index.php to run, and list the jokes, but in both cases, an error message appears at the top of the page:

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\byodb\index.php:10) in C:\Program Files\xampp\htdocs\byodb\index.php on line 63

Could it be that something happening in the wrong order?

A bit more info: The error / warning message goes away and the scripts appear to execute properly after removing the header(‘Location: .’) statement (at line 63 in index.php) along with the exit statement.

The problem isn’t the header() on line 63 of index.php, but that line 10 of index.php is producing output. Anytime anything is output, headers will be sent if they haven’t already been sent.

Thanks Mittineague. It’s still a mystery to me, but I’ll review all of my php script which starts at line 10, and see if I can find something.

spikeZ, that fixed it. I removed all of the html header (and tail) leaving just the php in my index.php and now it works fine. Previous to this I thought it might have been the UTF8 BOM encoding issue mentioned elsewhere, but apparently that wasn’t the complete solution in my case.

Thanks for the help.

thanks spikeZ, you also solved my problem. Thanks

you are welcome!