Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Location
    Florida
    Posts
    155
    Thanks
    16
    Thanked 2 Times in 2 Posts

    Text won't show on the page.

    Hello there.
    I have been building a simple blog system out of a book name PHP: For Absolute Beginners and the name is exactly right. Well, I have all the pages done, and it works mostly. When I save the new post, and navigate to the webpage, it doesn't show. Just the back link (refer to the link at the bottom.
    Here are all the associated pages. Please look over this. If you can find the bug, would you explain why it isn't working?
    index.php
    PHP Code:
    <?php include("tophead.php"); ?>
    <?php 
    include("header.php"); ?>
    <?php 
          
    include_once 'admin/inc/functions.inc.php';
          include_once 
    'admin/inc/db.inc.php';
          
          
    $db = new PDO(DB_INFODB_USERDB_PASS);
          
          
    $id = (isset($_GET['id'])) ? (int) $_GET['id'] : NULL;
          
          
    $e retrieveEntries($db$id);
          
          
    $fulldisp array_pop($e);
          
          
    $e sanitizeData($e);
    ?>
    <br />
    <div id="entries">
    <?php
    if($fulldisp==1)
    {
        
    ?>
    <h2> <?php echo $e['title'?> </h2>
    <p> <?php echo $e['entry'?> </p>
    <p class="backlink">
    <a href="./">Back to Latest Entries</a>
    </p>
    <?php
    }
    ?>
    </div>
    </body>
    </html>
    functions.inc.php
    PHP Code:
    <?php

    function retrieveEntries($db$id=NULL)
    {
         
    // Get entries from database
         
    if(isset($id))
         {
           
    // Load specified entry
           
    $sql "SELECT title, entry
                   FROM entries
                   WHERE id=?
                   LIMIT 1"
    ;
           
    $stmt $db->prepare($sql);
           
    $stmt->execute(array($_GET['id']));
           
           
    // Save the returned entry array
           
    $e $stmt->fetch();
           
           
    // Set the fulldisp flag for a single entry
           
    $fulldisp 1;
         }
         else
         {
           
    // Load all entry titles
           
    $sql "SELECT id, title
                   FROM entries
                   ORDER BY created DESC"
    ;
           foreach(
    $db->query($sql) as $row) {
               
    $e[] = array(
                   
    'id' => $row['id'],
                   
    'title' => $row['title']
               );
         }
         
         
    // Set the fulldisp flag for multiple entries
         
    $fulldisp 0;
         
         if(!
    is_array($e))
         {
            
    $fulldisp 1;
            
    $e = array(
                 
    'title' => 'No Entries Yet',
                 
    'entry' => '<a href="/admin.php">Post an entry!</a>'
            
    );
         }
         }
         
         
    // Return loaded data
         
    array_push($e$fulldisp);
         
         return 
    $e;
    }
    function 
    sanitizeData($data)
    {
      if(!
    is_array($data))
      {
      
        return 
    strip_tags($data"<a>");
      }
      
      else
      {
      
        return 
    array_map('sanitizeData'$data);
      }
    }

    ?>
    update.inc.php
    PHP Code:
    <?php

    if($_SERVER['REQUEST_METHOD']=='POST'
        
    && $_POST['submit']=='Save Entry'
        
    && !empty($_POST['title'])
        && !empty(
    $_POST['entry']))
    {
        
    // Include database credentials and connect to the database
        
    include_once 'db.inc.php';
        
    $db = new PDO(DB_INFODB_USERDB_PASS);
        
        
    // Save the entry into the database
        
    $sql "INSERT INTO entries (title, entry) VALUES (?, ?)";
        
    $stmt $db->prepare($sql);
        
    $stmt->execute(array($title$entry));
        
    $stmt->closeCursor();
        
        
    // Get the ID of the entry we just saved
        
    $id_obj $db->query("SELECT LAST_INSERT_ID()");
        
    $id $id_obj->fetch();
        
    $id_obj->closeCursor();
        
        
    // Send the user to the new entry
        
    header('Location: ../adminwrite.php?id='.$id[0]);
        exit;
        
    // Continue processing information . . .
    }

    // If both conditions aren't met, sends the user back to the main page
    else
    {
        
    header('Location: ../adminwrite.php');
        exit;
    }

    ?>
    Link to database pic http://newestfunny.com/db.jpg
    The only thing I can think of that is wrong is it is saving the text in the database incorrectly. But I have no idea.

    Again, thank you a bunch! As you can see on my clock, it is very late.

  • #2
    Regular Coder
    Join Date
    Oct 2009
    Location
    Florida
    Posts
    155
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Just tried again and got a new message:

    "Warning: Wrong parameter count for array_pop() in functions.inc.php on line 49"

    I am so lost

  • #3
    New Coder
    Join Date
    Jan 2006
    Posts
    73
    Thanks
    2
    Thanked 3 Times in 3 Posts
    In functions.inc.php:
    PHP Code:
    $sql "SELECT title, entry
                   FROM entries
                   WHERE id=?
                   LIMIT 1"

    and
    PHP Code:
    $sql "INSERT INTO entries (title, entry) VALUES (?, ?)"
    In update.inc.php:
    PHP Code:
    $sql "INSERT INTO entries (title, entry) VALUES (?, ?)"
    Those question marks are place holders for the real values to be inserted.

    Do this:
    PHP Code:
    <?php

    $title 
    $_POST['title'];
    $entry $_POST['entry'];
    then replace the VALUES section of the INSERT INTO query with
    PHP Code:
    VALUES ($title$entry)"; 
    It's quite simple, see if you can figure out the rest!

  • #4
    Regular Coder
    Join Date
    Oct 2009
    Location
    Florida
    Posts
    155
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Thank you for pointed that out. But that hasn't changed the result any. Now, whenever I add a new post. I always had domain.com/?=0 which isn't good and nothing shows in the database. *holds lighter to the book*

  • #5
    New Coder
    Join Date
    Jan 2006
    Posts
    73
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Check to see if the data is inserted properly now. There are quite a few pages you are leaving out, from what I can see.

    PHP Code:
    $sql "SELECT title, entry
                   FROM entries
                   WHERE id=?
                   LIMIT 1"

    id=? should be id=$id by the way.

  • #6
    Regular Coder
    Join Date
    Oct 2009
    Location
    Florida
    Posts
    155
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Hmmmm, I haven't left anything out that I know of. Those are all the page

    If it's related in any way, If I put the data in manually through the "INSERT" tab in phpmyadmin, it displays if I go to the url. But it won't via the form.

  • #7
    Regular Coder
    Join Date
    Oct 2009
    Location
    Florida
    Posts
    155
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Ok here is an update:

    If I insert the text manually through phpmyadmin, it will display if I go to the url. If I leave the part in update.inc.php Where is says: VALUES (?, ?) it will save the entry into the database but it will save it as NULL ( I don't know what the means) but if I change it to the way you said to, it doesn't save anything.


  •  

    Posting Permissions

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