SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2001
    Location
    North East, England
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trouble with joke db tutorial

    Hi, I've been working through the tutorial by by Kevin Yank using php and mysql. I have the first bit working and I get a display of the jokes in the jokes database. When I click the link to add a joke the form appears. The problems start when I submit the joke. The text dissapears but I do not get either:

    The confirmation message 'your joke has been added' or the other message 'error adding submitted joke'.

    Also the joke is not added to my database and I get no error messages of any kind. I have put the entire code below and apreciate any help.
    Thanks Lee.

    <!doctype html public "-//W3C//DTD HTML 4.0 //EN">
    <html>
    <head>
    <title>Jokes</title>
    </head>
    <body>

    <?php
    //If the user wants to add a joke
    if (isset($addjoke)):
    ?>

    <form action="<?php echo($php_self); ?>" method=post>
    <p>Type your joke herebr>
    <textarea name="joketext" rows=10 cols=40 wrap>
    </textarea><br>
    <input type=submit name="submitjoke" value="submit">
    </form>

    <?php
    else:
    //Conect to the database server
    $dbcnx = @mysql_connect("localhost", "username", "password");
    if (!$dbcnx) {
    echo("<p>Unable to connect to the " .
    "database server at this time.</p>");
    exit();
    }

    //Select the jokes database
    if (! @mysql_select_db("jokes") ) {
    echo("<p>Unable to locate the joke " .
    "database at this time.</p>");
    exit();
    }

    //If a joke has been submitted, add it to the database
    if ("submit" == $submitjoke) {
    $sql = "insert into jokes set " .
    "joketext='$joketext', " .
    "jokedate=CURDATE()";
    if (mysql_query($sql)) {
    echo("<p>Your joke has been added</p>");
    }
    else {
    echo("<p>Error adding submitted joke: ") .
    mysql_error() . "</p>";
    }
    }

    echo("<p>Here are all the jokes " .
    "in our database: </p>");

    //Request the text of all the jokes
    $result = mysql_query("select joketext from jokes");
    if (!$result) {
    echo("<p>Error performing query: " .
    mysql_error() . "</p>");
    exit();
    }

    //Display the text of each joke in a paragraph
    while ($row = mysql_fetch_array($result)) {
    echo("<p>" . $row["joketext"] . "</p>");
    }

    //When clicked, this link will load this page
    //with the joke submition form displayed
    echo("<p> <a href='$php_self?addjoke=1'>" .
    "Add a joke!</a></p>");

    endif;

    ?>

    </body>
    </html>

  2. #2
    SitePoint Wizard
    Join Date
    Jul 2001
    Location
    The Netherlands
    Posts
    2,617
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure, but try this. Change this part:

    PHP Code:
    //If a joke has been submitted, add it to the database 
    if ("submit" == $submitjoke) { 
    $sql "insert into jokes set " 
    "joketext='$joketext', " 
    "jokedate=CURDATE()"
    if (
    mysql_query($sql)) { 
    echo(
    "<p>Your joke has been added</p>"); 

    else { 
    echo(
    "<p>Error adding submitted joke: ") . 
    mysql_error() . "</p>"


    To this one:

    PHP Code:
    //If a joke has been submitted, add it to the database 
    if ("submit" == $submitjoke) { 
    $sql "insert into jokes set " 
    "joketext='$joketext', " 
    "jokedate=CURDATE()";
    $result mysql_query($sql); 
    if (
    $result)  { 
    echo(
    "<p>Your joke has been added</p>"); 

    else { 
    echo(
    "<p>Error adding submitted joke: ") . 
    mysql_error() . "</p>"


    I am not sure if it works, but let me explain what I altered. I inserted a line executing the sql-query, since I was not sure this happened in the if-structure. The outcome of this became a variable called $result, and then you can use this variable to check whether the joke was sent or not.

    If this doesn't work, try checking if your sql-query is correct. IMO the commands, like INSERT and SET, had to be written in CAPITALS in stead of lowercase. Just a thought.

    I hope this helps you out and good luck with the tutorial .

  3. #3
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Trouble with joke db tutorial

    Think your problem may be here:

    <form action="<?php echo($php_self); ?>" method=post>
    <p>Type your joke herebr>...
    Have had problems myself if "$PHP_SELF" is written in lowercase. Course I may just have a hole in the head - perhaps one of the pros can confirm

  4. #4
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always write $PHP_SELF in caps like that, so I can't comment, but:
    <form action="<?php echo($php_self); ?>" method=post>

    Be consistant with your quote use, and you should use quotes around the attributes as it is correct HTML and XHTML, also, there is a 'short-hand' way of putting the $PHP_SELF in here... so:

    <form action="<?=$PHP_SELF?>" method="post">
    Oh no! the coots are eating my nodes!

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2001
    Location
    North East, England
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot. As soon as I changed the $php_self part in

    <form action="<?php echo($PHP_SELF); ?>" method="post">

    to uppercase it worked (sort of)

    I now get the joke added to the database and then the page showing the full list of jokes in my database. I do not however get the confimation message that my joke has been added.

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2001
    Location
    North East, England
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sory about the last post. It appears that I do get the confirmation message at the top of my list of jokes, It just blends in with the list of jokes so I overlooked it. Thanks again for your replies
    Lee.


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
  •