Kevin Yank php mysql 4th edition no new jokes showing up

Totally new to PHP: Going through “Build Your Own Database Driven Web Site Using PHP & MySQL” 4th Edition

I have reached page 162 where you have the following functionality.

Delete button-- This works correctly removing jokes from the database
“Add your own joke” — This works correctly because I can verify new jokes in the database
The two tables Author and Jokes (innerjoined) Display correctly on the page

Everything works but new jokes dont show up. In fact only 4 older jokes show up

I can look and verify that new jokes are in the database , everything but the display of new jokes is working

i am sure that was a lame way to express my problem so go ahead and flame the hell out of me now, any input would be appreciated

Ok I read throught the forums and added

<?php
ini_set(‘display_errors’,1);
error_reporting(E_ALL);
?>

to the top of my controller index.php and that returned an error at line 64 of index.php that line reads

header(‘Location: .’);

OK I guess I really dont know where to start so I am looking for basic debugging? Does that make sense? Still cruising the forums.

how about some code so we know what we are dealing with?

this is index.php


<?php 
ini_set('display_errors',1); 
error_reporting(E_ALL); 
?>

<?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', 'password'); 
 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, 'blackbear')) 
       { 
         $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 joke.id, joketext, name, email 
      FROM joke INNER JOIN author 
         ON authorid = author.id'); 
       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'],
        'name' => $row['name'], 'email' => $row['email']); 
  } 

 include 'jokes.html.php'; 
 ?> 

this is form.html.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>Add Joke</title> 
             <meta http-equiv="content-type" 
                  content="text/html; charset=utf-8"/> 
             <style type="text/css"> 
             textarea { 
                display: block; 
                width: 100%; 
             } 
             </style> 
           </head> 
           <body> 
             <form action="?" method="post"> 
                <div> 
                  <label for="joketext">Type your joke here:</label> 
                  <textarea id="joketext" name="joketext" rows="3" cols="40"> 
        â&#382;¥</textarea> 
                </div> 
                <div><input type="submit" value="Add"/></div> 
             </form> 
           </body> 
         </html> 

this is jokes.html.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>List of Jokes</title> 
             <meta http-equiv="content-type" 
                  content="text/html; charset=utf-8"/> 
           </head> 
           <body>  
    <p><a href="?addjoke">Add your own joke</a></p> 
    <p>Here are all the jokes in the database:</p>
             
     <?php foreach ($jokes as $joke): ?> 
    <form action="?deletejoke" method="post"> 
      <blockquote> 
         <p> 
           <?php echo htmlspecialchars($joke['text'], ENT_QUOTES, 
                'UTF-8'); ?> 
           <input type="hidden" name="id" value="<?php 
                echo $joke['id']; ?>"/> 
           <input type="submit" value="Delete"/> 
           (by <a href="mailto:<?php 
                echo htmlspecialchars($joke['email'], ENT_QUOTES, 
                     'UTF-8'); ?>"><?php 
                echo htmlspecialchars($joke['name'], ENT_QUOTES, 
                     'UTF-8'); ?></a>) 
         </p> 
      </blockquote> 
    </form> 
 <?php endforeach; ?> 

           </body>
        </html>

this is error.html.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>PHP Error</title> 
<meta http-equiv="content-type" 
           content="text/html; charset=utf-8"/> 
    </head> 
    <body> 
      <p> 
         <?php echo $error; ?> 
      </p> 
    </body> 
 </html> 

I am looking for a screen capture I can do lower res so I dont exceed the attachment limits for this site. But re-stated as best I can …There is a delete button beside each joke. that works. There is an author from the database and 4 jokes show from the data base. There is an “Add Joke” link and that works. I can verify the jokes get in the database but it should be displaying the full list of jokes from the database.

‘SELECT joke.id, joketext, name, email
FROM joke INNER JOIN author
ON authorid = author.id’

you are joining on author id, from what i see your form is only adding jokes to the database, with no other info
so, if the joke is in the database and there is no author id, your query will not return the joke

That makes a lot of sense. Thanks so much for your time. I will work with that and report back. A thousand thank yous.

I understand you to be saying jokes.html.php when you say the form

you said form, so maybe it doesn require sherlock holmes to deduce you meant form, I put that together just now by myself :slight_smile:

Dear Sir you could not have been more correct. I was looking for functionality that is not there yet…oh boy…that comes later in the tutorial. There is no way at this point (later in the book there is) to enter authors (for example). Well it was useful poking around the code and tracing what you said was wrong for myself. Thanks again. Pardon the invented problem. Thanks!!!

I was going to say something about setting a default value for the author, but that is for another lesson :slight_smile:

you could actually get the info you want by omitting the JOIN :slight_smile:

Glad I could help.
That is a great book for learning the basics of PHP, good luck with your journey through it.