Announcement

Collapse
No announcement yet.

Undefined Variables in tutorial

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Undefined Variables in tutorial

    I just came to my last lesson in Chris Cosentino's "Essential PHP", (More Robust Data-Driven Applications), and have spent the entire day trying to piece together my brief understanding of the language and some very unhelpful online resources, to solve this script. I am convinced that Cosentino deliberately programmed bugs into his scripts so that people that didn't labourously type in key by key (unlike me) would still have to go out and learn something. It is apparent in most of his examples, which can be found at http://www.phptr.com/essentials.

    I tested his exact, downloadable script out for this, and it shows the same errors.

    My script reads as follows:

    -------

    <?php

    /*START FUNCTION DECS */

    function connect() {
    if(!$db = mysql_connect("localhost", "root")):
    print("<h1>Can't connect to the DB!</h1>\n");
    else:
    mysql_select_db("um", $db);
    endif;
    }

    function head($title) {
    ?>

    <html>
    <head>
    <title><? echo $title ?></title>

    <style type="text/css">
    BODY {color: White;
    background-color: Black;
    font-family: Tahoma, Arial, Verdana, sans-serif;
    font-size: 2;}

    TD { font-family: Tahoma, Arial, Verdana;}

    A { color: Teal;
    text-decoration: none;}

    A:HOVER {color: #FF4500;}
    </style>

    </head>

    <body bgcolor="#000000" text="#ffffff">

    <table width="420" border="0" align="CENTER">
    <tr><td>&nbsp;
    <hr align="LEFT" size="1" width="420" noshade>
    <h1>Shock News</h1>
    <hr align="LEFT" size="1" width="420" noshade>
    </td></tr>
    </table>

    <?
    }

    function write_post(){

    ?>

    <h2>Write a New Article</h2>

    <form action="shockdot.php3" method="POST">
    <p>Author: <br><input type="text" name="author">
    <p>Password: <br><input type="password" name="password">
    <p>Category: <br><? catList(); ?>
    <p>Title: <br><input type="text" name="title" size="36" maxlength="36">
    <p>Intro: <br><textarea cols=75 rows=5 name="intro"></textarea>
    <p>More: <br><textarea cols=75 rows=15 name="more"></textarea>

    <p><input type="submit" name="submit">
    </form>

    <?
    }

    function enter_post($author, $password, $title, $intro, $more, $category) {
    connect();

    $sql = "select * from authors where author_name = '$author'";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);

    if($row["author_password"] != $password):
    print("<h1>Wrong Password!</h1>");
    else:
    $author = $row["author_id"];
    $time = date("H:i:s");
    $date = date("y-m-d");
    $sql = "insert into news2 VALUES(NULL, '$title', '$intro', '$more', '$author', '$category', '$date', '$time')";
    mysql_query($sql);
    endif;
    }

    function display($show_what) {
    switch ($show_what) {
    case "":
    $sql = "select * from news2 where(TO_DAYS(NOW())-TO_DAYS(date)) <= '1' ORDER BY date DESC, time DESC";
    break;
    default:
    $sql = "select * from news2 where (TO_DAYS(NOW())-TO_DAYS(date)) <= '1' ORDER BY date DESC";
    break;
    }
    display_posts($sql);
    }

    function display_posts($sql) {
    connect();
    $result = mysql_query($sql);
    ?>

    <table width="400" border="0" cellpadding="5" align="center">
    <tr><td>
    <hr align="LEFT" size="1" width="400" noshade>
    </td></tr>

    <?
    while($row = mysql_fetch_array($result)) {

    ?>

    <tr><td>

    <?

    if($last_date == $row["date"]):

    ?>

    <p class="date"><b><? echo $row["time"] ?> </b>

    <? else: ?>

    <p class="date"><? echo $row["time"] ?> - <b>
    <? echo $row["date"] ?> </b>

    <? endif;

    $cat_num = $row["category_id"];
    $cat_result = mysql_query("select categories.category from news2, categories where news2.category_id = categories.categories_id and news2.category_id = '$cat_num'");

    $cat = mysql_fetch_array($cat_result);
    ?>

    <br>Category: <i><? echo $cat["category"] ?></i>

    <p><? echo "<b>".$row["title"]."</b>\n" ?>
    <p><? echo $row["intro"] ?><br>

    <?
    printf("\n<p><a href=\"shockdot.php3?more=%s\">
    [<i>More...</i></a>\n", $row["news_id"]);
    printf("\n <a href=\"shockdot.php3?add_comment=%s\">[<i>Add Comment...</i>]</a>\n", $row["news_id"]);
    $last_date = $row["date"];
    ?>

    <hr align="right" size="1" width="400" noshade>

    </td></tr>

    <?
    }
    ?> </table> <?

    }

    function catList() {
    connect() ?>
    <select name="category">

    <?

    $sql = "select * from categories";
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result)) {
    printf("<option value=\"%s\">%s</option>", $row["category_id"], $row["category"]);
    }

    ?>

    </select>

    <?
    }
    function more($more) {
    connect();
    $sql = "select * from news2 where news_id = '$more'";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);

    ?>

    <table width="400" border="0" cellpadding="5" align="center">
    <tr><td>

    <hr align="right" size="1" width="400" noshade>

    <p class="date"><? echo $row["time"] ?> - <b><? echo $row["date"] ?></b>

    <?

    $cat_num = $row["category_id"];
    $cat_result = mysql_query("select categories.category from news2, categories where news2.category_id = categories.category_id and news2.category_id = '$cat_num'");
    $cat = mysql_fetch_array($cat_result);

    ?>

    <br>
    Category: <i><? echo $cat ["category"] ?> </i>
    <p><? echo "<b>".$row["title"]."</b>" ?>
    <p><? echo "<i>" . $row["intro"] . "</i>" ?>
    <p><? echo "---<p>" . $row["more"] ?>

    <hr align="right" size="1" width="400" noshade>

    </td></tr>
    </table>

    <?
    }

    function show_comments($id) {
    $sql = "select * from comments where news_id = '$id'";
    $result = mysql_query($sql);
    $cnt = mysql_num_rows($result);
    print("<center><p><b>There are $cnt comments for this post.</b></center>\n");

    if($cnt > '0'):

    ?>

    <table width="390" border="1" cellspacing="0" cellpadding="5" align="center">

    <?

    while($row = mysql_fetch_array($result)) {
    $user_id = $row["user_id"];
    $sql2 = "select * from users where user_id = '$user_id'";

    $result2 = mysql_query($sql2);
    $row2 = mysql_fetch_array($result2);
    $comments = stripslashes($row["comments"]);
    printf("<tr><td><i>Comment by %s:</i><br><pre>%s</pre></td></tr>", $row2["user_name"], $comments);

    }

    ?>

    <tr><td>

    <?

    printf("\n <a href=\"shockdot.php3?add_comment=%s\">[<i>Add Comment...</i>]</a>\n", $id);

    ?>

    </td></tr>

    </table>

    <?

    endif;
    }


    function add_comment ($id) {
    ?>

    <center>
    <form action="shockdot.php3" method="POST">
    <h2>Add a comment to this news article:</h2>
    <p>Not registered? Then <a href="shockdot.php3?signup=newuser">Sign up!</a>

    <input type="hidden" name="news_id" value="<? echo $id ?>">
    <p>Username: <br><input type="text" name="user_name">
    <p>Password: <br><input type="password" name="password">
    <p>Comments: <br><textarea cols=40 rows=10 name="comments"></textarea>
    <br>
    <input type="submit" name="insert_comments" value="Add your comments">
    </form>

    </center>

    <?

    }

    function insert_comments ($news_id, $user_name, $password, $comments) {

    connect();
    $sql = "select * from users where user_name = '$user_name'";
    $result = mysql_query($sql);
    $row_count = mysql_num_rows($result);
    if($row_count == 0):

    ?>

    <h3>Wrong Username! Click the browser's Back button and try, try again.</h3>

    <?

    else:
    $row = mysql_fetch_array($result);
    if($password != $row["user_password"]):

    ?>

    <h3>Incorrect Password! Click the browser's Back button and try again.</h3>

    <?

    else:

    ?>

    <center><h3>Thanks for your comments!</h3></center>

    <?

    $user_id = $row["user_id"];
    $comments = addslashes($comments);
    $sql2 = "insert into comments values (NULL, '$news_id', '$user_id', '$comments')";
    mysql_query($sql2);
    more($news_id);
    show_comments($news_id);
    endif;
    endif;
    }

    function signup($signup, $user_name, $user_email, $user_password){
    if($signup == "enter_user"):
    connect();
    $sql = "insert into users values (NULL, '$user_name', '$user_email', '$user_password')";
    mysql_query($sql);
    print("<h2>Thanks for Signing Up!</h2>");
    else:

    ?>

    <p>Please fill out this short registration form to add comments to our news articles:
    <form action="shockdot.php3?signup=enter_user" method="POST">
    <input type="hidden" name="signup" value="enter_user">
    <p>User Name: <input type="text" name="user_name">
    <br>Email: <input type="text" name="user_email">
    <br>Password: <input type="password" name="user_password" size="8" maxlength="8">
    <p><input type="submit" name="newuser" value="Signup!">

    </form>

    <?

    endif;

    }

    /* END FUNCTION DECS */

    /* START MAIN PROGRAM */

    if(isset($submit)):
    head("New Article Entry");
    enter_post($author, $password, $title, $intro, $more, $category);
    display($show_what);
    elseif(isset($more)):
    head("More of the story...");
    more($more);
    show_comments($more);
    elseif(isset($add_comment)):
    head("Add Your Comments...");
    more($add_comment);
    add_comment($add_comment);
    elseif(isset($insert_comments)):
    head("Thanks for your comments!");
    insert_comments($news_id, $user_name, $password, $comments);
    elseif(isset($signup)):
    head("Signup to Post Comments");
    signup($signup, $user_name, $user_email, $user_password);
    elseif($action == "post"):
    head("Write a new article");
    write_post();
    else:
    head("Shock Magazine News Stories");
    display($show_what);
    endif;


    /* END MAIN PROGRAM */

    ?>
    </body>
    </html>

    -------

    (mySQL db = 'um')

    the errors that I receive in this script are:

    -------

    <b>Warning</b>: Undefined variable: action in <b>c:\program files\apache group\apache\htdocs\php\shockdot.php3</b> on line <b>372</b><br>

    and

    <b>Warning</b>: Undefined variable: show_what in <b>c:\program files\apache group\apache\htdocs\php\shockdot.php3</b> on line <b>377</b>


    -------

    I am somewhat confused by the "Switch" statements, that seemed to come out of the blue for this final lesson.

    Now, I don't just want to solve this, I want to know the line of thought towards correcting it, and what prevented it from working in its current state. And why Mr. Consentino is a very mean person.

    Many pleases and thank you's...
widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
Working...
X