Professional Web Applications Themes

discussion forum from j. meloni's book - PHP Development

The following code is From Julie Meloni's textbook - PHP, mySQL and Apache (SAMS) on page 305-307 / Listing 14.5: It's the last script of a discussion forum which is comprised of four php files and one html file. The script can be seen at: [url]http://www.thickbook.com[/url] Chapter 14 off the zipfile. Anyway, in the textbook and the zip her mySQL query reads: .................... $verify = "select ft.topic_id, ft.topic_title from forum_posts as fp left join forum_topics as ft on fp.topic_id = ft.topic_id where fp.post_id = $_GET[post_id]"; .................. The part that I'm confused about is: ................ select ft.topic_id, ft.topic_title from forum_posts ................. ...

  1. #1

    Default discussion forum from j. meloni's book

    The following code is From Julie Meloni's textbook - PHP, mySQL and Apache
    (SAMS) on page 305-307 / Listing 14.5:

    It's the last script of a discussion forum which is comprised of four php
    files and one html file.

    The script can be seen at:
    [url]http://www.thickbook.com[/url]

    Chapter 14 off the zipfile.

    Anyway, in the textbook and the zip her mySQL query reads:
    ....................
    $verify = "select ft.topic_id, ft.topic_title from forum_posts as fp left
    join forum_topics as ft on fp.topic_id = ft.topic_id where fp.post_id =
    $_GET[post_id]";
    ..................

    The part that I'm confused about is:
    ................
    select ft.topic_id, ft.topic_title from forum_posts
    .................

    It seem that the alias of ft refers to forum_title - not forum_posts.

    In fact, I switched the query to read as follows:
    .................
    $verify = "select ft.topic_id, ft.topic_title from forum_topics as ft left
    join forum_posts as fp on fp.topic_id = ft.topic_id where fp.post_id =
    $_GET[post_id]";
    ...................

    And that seems to run fine.

    Any explanations will be appreciated.

    Her original code is below.

    Thank you.

    Tony Ritter

    ...........................


    <?php
    //connect to server and select database; we'll need it soon
    $conn = mysql_connect("localhost", "root", "thisisapassword") or
    die(mysql_error());
    mysql_select_db("sitename",$conn) or die(mysql_error());

    //check to see if we're showing the form or adding the post
    if ($_POST[op] != "addpost") {
    // showing the form; check for required item in query string
    if (!$_GET[post_id]) {
    header("Location: topiclist.php");
    exit;
    }

    //still have to verify topic and post
    $verify = "select ft.topic_id, ft.topic_title from forum_topics as ft
    left join forum_posts as fp on fp.topic_id = ft.topic_id where fp.post_id =
    $_GET[post_id]";
    $verify_res = mysql_query($verify, $conn) or die(mysql_error());
    if (mysql_num_rows($verify_res) < 1) {
    //this post or topic does not exist
    header("Location: topiclist.php");
    exit;
    } else {
    //get the topic id and title
    $topic_id = mysql_result($verify_res,0,'topic_id');
    $topic_title = stripslashes(mysql_result($verify_res,
    0,'topic_title'));

    print "
    <html>
    <head>
    <title>Post Your Reply in $topic_title</title>
    </head>
    <body>
    <h1>Post Your Reply in $topic_title</h1>
    <form method=post action=\"$_SERVER[PHP_SELF]\">
    <p><strong>Your E-Mail Address:</strong><br>
    <input type=\"text\" name=\"post_owner\" size=40 maxlength=150>

    <P><strong>Post Text:</strong><br>
    <textarea name=\"post_text\" rows=8 cols=40 wrap=virtual></textarea>

    <input type=\"hidden\" name=\"op\" value=\"addpost\">
    <input type=\"hidden\" name=\"topic_id\" value=\"$topic_id\">

    <P><input type=\"submit\" name=\"submit\" value=\"Add Post\"></p>

    </form>
    </body>
    </html>";
    }
    } else if ($_POST[op] == "addpost") {
    //check for required items from form
    if ((!$_POST[topic_id]) || (!$_POST[post_text]) || (!$_POST[post_owner]))
    {
    header("Location: topiclist.php");
    exit;
    }

    //add the post
    $add_post = "insert into forum_posts values ('', '$_POST[topic_id]',
    '$_POST[post_text]', now(), '$_POST[post_owner]')";
    mysql_query($add_post,$conn) or die(mysql_error());

    //redirect user to topic
    header("Location: showtopic.php?topic_id=$topic_id");
    exit;
    }
    ?>



    Anthony Ritter Guest

  2. #2

    Default RE: [PHP] discussion forum from j. meloni's book

    > Anyway, in the textbook and the zip her mySQL query reads:
    > ...................
    > $verify = "select ft.topic_id, ft.topic_title from forum_posts as fp left
    > join forum_topics as ft on fp.topic_id = ft.topic_id where fp.post_id =
    > $_GET[post_id]";
    > .................
    >
    > The part that I'm confused about is:
    > ...............
    > select ft.topic_id, ft.topic_title from forum_posts
    > ................
    >
    > It seem that the alias of ft refers to forum_title - not forum_posts.
    ft refers to forum_topics "forum_topics as ft "
    >
    > In fact, I switched the query to read as follows:
    > ................
    > $verify = "select ft.topic_id, ft.topic_title from forum_topics as ft left
    > join forum_posts as fp on fp.topic_id = ft.topic_id where fp.post_id =
    > $_GET[post_id]";
    > ..................
    >
    > And that seems to run fine.
    >
    > Any explanations will be appreciated.
    You didn't switch the aliases around, you just switched the join order.
    This will provide unexpected results. In order to understand it, you should
    read up on left joins.
    [url]http://www.mysql.com/doc/en/JOIN.html[/url]


    Jennifer Goodie Guest

  3. #3

    Default Re: [PHP] discussion forum from j. meloni's book

    Jennifer Goodie" <goodieapollointeractive.com> writes:
    > You didn't switch the aliases around, you just switched the join order.
    > This will provide unexpected results. In order to understand it, you
    should
    > read up on left joins.
    > [url]http://www.mysql.com/doc/en/JOIN.html[/url]
    .............................
    Thank you. But...

    // the query

    $verify = "select ft.topic_id, ft.topic_title from forum_posts as fp left
    join forum_topics as ft on fp.topic_id = ft.topic_id where fp.post_id =
    $_GET[post_id]";
    ............................

    My question: why - or how can - the columns ft.topic_id and ft.topic_title
    come from the table forum_posts?

    In the original schema, there is no column called topic_title in the table
    called forum_posts.

    When I change the following to read:

    $verify = "select ft.topic_id, ft.topic_title from forum_topics as ft left
    join forum_posts as fp on fp.topic_id = ft.topic_id where fp.post_id =
    $_GET[post_id]";

    It works fine.

    /*Roughly translated: Select the column ft.topic_id along with the column
    ft.topic_title from the table called forum_topics which we'll call ft. Then
    join the table called forum_topics onto the table called forum_posts and
    match the records where topic_id corresponds in both tables along with
    post_id corresponds in both tables.*/



    Anthony Ritter Guest

  4. #4

    Default Re: [PHP] discussion forum from j. meloni's book

    * Thus wrote Anthony Ritter (tonygonefishingguideservice.com):
    > Jennifer Goodie" <goodieapollointeractive.com> writes:
    >
    > > You didn't switch the aliases around, you just switched the join order.
    > > This will provide unexpected results. In order to understand it, you
    > should
    > > read up on left joins.
    > > [url]http://www.mysql.com/doc/en/JOIN.html[/url]
    > ............................
    > Thank you. But...
    >
    > // the query
    >
    > $verify = "select ft.topic_id, ft.topic_title from forum_posts as fp left
    > join forum_topics as ft on fp.topic_id = ft.topic_id where fp.post_id =
    > $_GET[post_id]";
    > ...........................
    >
    > My question: why - or how can - the columns ft.topic_id and ft.topic_title
    > come from the table forum_posts?
    The dont. ft is aliased to forum_topic it has nothing to do with
    form_posts.
    >
    >
    > When I change the following to read:
    >
    > $verify = "select ft.topic_id, ft.topic_title from forum_topics as ft left
    > join forum_posts as fp on fp.topic_id = ft.topic_id where fp.post_id =
    > $_GET[post_id]";
    >
    > It works fine.
    >
    > /*Roughly translated: Select the column ft.topic_id along with the column
    > ft.topic_title from the table called forum_topics which we'll call ft. Then
    > join the table called forum_topics onto the table called forum_posts and
    > match the records where topic_id corresponds in both tables along with
    > post_id corresponds in both tables.*/
    There are a lot of thing wrong with your summary. What you are
    doing is selecting *all* rows from ft. include any rows from fp that
    have the same topic_id where fp has a post_id of the $_GET
    variable.

    This query makes absolutly no sense, in real world application.

    btw, this topic is completely off-topic, you might be able to get
    better assistance with this query on the mysql list.

    Curt
    --
    "I used to think I was indecisive, but now I'm not so sure."
    Curt Zirzow Guest

Similar Threads

  1. Discussion Forum
    By Bobbie in forum Coldfusion Database Access
    Replies: 5
    Last Post: June 3rd, 04:02 PM
  2. Hosting a Discussion Forum
    By GreatPaulC in forum Macromedia Dynamic HTML
    Replies: 6
    Last Post: November 6th, 11:31 AM
  3. [PHP] discussion forum from j. meloni's book
    By Jennifer Goodie in forum PHP Development
    Replies: 4
    Last Post: August 13th, 01:04 PM
  4. discussion forum using dreamweaver mx
    By jonnyfriendly webforumsuser@macromedia.com in forum Macromedia Dreamweaver
    Replies: 7
    Last Post: July 26th, 08:19 AM
  5. discussion forum design
    By hj in forum PHP Development
    Replies: 3
    Last Post: July 25th, 07:25 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139