SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Dec 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if anyone has read 'PHP For bsolute Beginners' then please reply

    I am stuck in a chap 5 for a while now! i followed all the examples in the book to design a simple blog application in PHP but in chapter 5 i am not able to retrieve entries and title. I checked if the data was being stored in the database and not getting displayed , but it's not even getting stored in the data base.
    here's the index.php file code

    <?php

    include_once 'inc/functions.inc.php';
    include_once 'inc/db.inc.php';
    $db = new PDO(DB_INFO,DB_USER,DB_PASS);

    $id= (isset($_GET['id'])) ? (int) $_GET['id'] :NULL;
    $e=retrieveEntries($db,$id);

    // Get the fulldisp flag and remove it from the array
    $fulldisp = array_pop($e);
    // Sanitize the entry data
    $e = sanitizeData($e);

    ?>

    <!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>
    <meta http-equiv="Content-Type"
    content="text/html;charset=utf-8" />
    <link rel="stylesheet" href="/css/default.css" type="text/css" />
    <title> Simple Blog </title>
    </head>
    <body>
    <h1> Simple Blog Application </h1>
    <div id="entries">

    <?php
    if($fulldisp==1)
    {

    ?>

    <h2> <?php echo $e['title'] ?> </h2>
    <p> <?php echo $e['entries'] ?> </p>
    <p class="backlink">
    <a href="./">Back to the Latest Entries</a>
    </p>

    <?php

    }

    else
    {
    foreach($e as $entry) {

    ?>

    <p>
    <a href="?id=<?php echo $entry['id']?>">
    <?php echo $entry['title'] ?>

    </a>
    </p>

    <?php
    }
    }
    ?>


    <p class="backlink">
    <a href="admin.php">Post A New Entry!</a>
    </p>
    </div>



    here's the functions.inc.php code

    <?php

    function retrieveEntries ($db,$id=NULL)
    {
    if(isset($id))
    {
    $sql= "SELECT title,entry
    FROM entries
    WHERE id=?
    LIMIT 1";
    $stmt=$db->prepare($sql);
    $stmt->execute(array($_GET['id']));

    $e=$stmt->fetch();

    $fulldisp=1;
    }

    else
    {
    $sql= "SELECT id, title
    FROM entries
    ORDER BY created DESC";

    foreach($db->query($sql) as $row) {
    $e[]= array(
    'id'=> $row['id'],
    'title'=> $row['title']
    );
    }

    $fulldisp=0;

    if(!is_array($e))
    {
    $fulldisp=1;
    $e= array(
    'title'=> 'No Entries Yet',
    'entry'=>'<a href="/admin.php">Post an entry!</a>'
    );
    }
    }

    array_push($e, $fulldisp);
    return $e;

    }

    function sanitizeData($data)
    {
    if(!is_array($data))
    {
    return strip_tags($data, "<a>");
    }

    else
    {
    return array_map('sanitizeData', $data);
    }
    }
    ?>


    and here's the update.inc.php code

    <?php

    if($_SERVER['REQUEST_METHOD']=='POST'
    && $_POST['submit']=='Save Entry')

    {
    include_once 'db.inc.php';
    $db=new PDO(DB_INFO, DB_USER, DB_PASS);

    $sql= "INSERT INTO entries (title,entry) VALUES (?,?)";
    $stmt= $db->prepare($sql);
    $stmt-> execute(array($_POST['title'],$_POST['entry']));
    $stmt->closeCursor();


    $id_obj= $db->query("SELECT LAST_INSERT_ID()");
    $id= $id_obj->fetch();
    $id_obj->closeCursor();

    header('Location: ../?id='.$id[0]);
    exit;

    }

    else
    {
    header('Location: ../');
    exit;
    }

    ?>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,712
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    The errata page has some info about pages 133 and 135

    Give that a try.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Dec 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I tried the following script alos, but it shows nothing.

    I am just a naive php programmer , so I need a little help, I tried the following script:


    <?php

    $dbinfo='mysql:host=localhost;dbname=test';
    $user='root';
    $pass=' ';

    $link=new PDO($dbinfo,$user,$pass);

    if(!$link) {
    echo 'connection failed';
    }

    else {
    echo 'connection successful';
    }

    ?>


    Is the above script wrong?
    because if i try to open this file in the browser , it shows nothing!

  4. #4
    SitePoint Member
    Join Date
    Dec 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you create a mysql database named "test" for it? Was the username and password correct?
    Besides, try this: error_reporting(E_ALL); --> put it at the beginning of the php file.


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
  •