SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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: .');

  3. #3
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how about some code so we know what we are dealing with?

  5. #5
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is index.php

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

  6. #6
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is form.html.php

    Code 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>

  7. #7
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is jokes.html.php

    Code 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>

  8. #8
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is error.html.php

    Code 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>

  9. #9
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  10. #10
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Reddleman View Post
    this is index.php

    Code php:
    <?php 
     $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(); 
      } 
     
     
     ?>
    '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

  11. #11
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by craqgerbil View Post
    '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.

  12. #12
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I understand you to be saying jokes.html.php when you say the form

  13. #13
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you said form, so maybe it doesn require sherlock holmes to deduce you meant form, I put that together just now by myself :-)

  14. #14
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by craqgerbil View Post
    '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
    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!!!

  15. #15
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was going to say something about setting a default value for the author, but that is for another lesson

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

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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •