SitePoint Sponsor

User Tag List

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

    Database driven website

    Hi there,

    I m very new on this forum.

    I m on the chapter 4 of the book DATABASE DRIVEN WEBSITE and I m getting a bit frustrated because my codes don't work:-(

    Attached the file. Could someone help me out and tell me where the issue is ?

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Learning</title>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    </head>
    <body>
    <div id="container">

    <?php if(isset($_get['addjoke'])): ?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <label>Type your joke here:<br />
    <textarea name"joketext" rows="10" cols="40">
    </textarea></label><br />
    <input type="submit" value="SUBMIT" />
    </form>


    <?php else: //default page display

    // connect to db
    $dbcnx = @mysql_connect('localhost''root''root');
    if (!
    $dbcnx) {
        echo 
    '<p>Unable to connect to the ' 'database server at this time.</p>';
        exit();
        }

    if (!@
    mysql_select_db('ijdb')) {
        exit(
    '<p>Unable to select the joke ' 'database server at this time.</p>');
        }
    //if a joke has been added
    //add it to the db

    if (isset($_POST['joketext'])) {
        
    $joketext $_POST['joketext'];
        
    $sql "INSERT INTO joke 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>';
        }
    }

    if (isset(
    $_GET['deletjoke'])) {
        
    $jokeid $_GET['deletejok'];
        
    $sql "DELETE FROM joke
            WHERE id='
    $jokeid'";
        if (@
    mysql_query($sql)) {
        echo 
    '<p>The joke has been deleted.</p> ';
        } else {
        echo 
    '<p>Error deleting joke: ' .
        
    mysql_error() . '</p>';
        }
    }

    echo 
    '<p>Here are all the jokes in our database :</p>';

    // request the jokes from the db
    $result = @mysql_query('SELECT id, joketext FROM joke');
    if (!
    $result) {
     exit(
    '<p>Error performing query : ' mysql_error() . '</p>');
     }
     
     
    //display the text of each joke in a p
     
    while ($row mysql_fetch_array($result)) {
         
    $jokeid $row['id'];
         
    $joketext $row ['joketext'];
         echo 
    '<p>' .$joketext.
         
    '<p><a href="' $_SERVER['PHP_SELF'] .
         
    '?deletejoke=' .$jokeid '">' .
         
    'Delete this joke</a></p>';
    }


    //when click on this link, the form will appear
    echo '<p><a href="' $_SERVER['PHP SELF'] .
        
    '?addjoke=1">Add a Joke!</a></p>';

    endif;
    ?>
    </div>

    </body>
    </html>
    Thanks :-)

    Thierry
    Attached Files Attached Files
    Last edited by SpacePhoenix; Aug 28, 2011 at 15:13. Reason: copy and pasted php code from OP's attached file

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mullerthierry View Post
    my codes don't work:-(
    Welcome to the SP forums.
    Could you explain a bit more what "don't work" means? Do you get an error? A blank page?

  3. #3
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Welcome to the SP forums.
    Could you explain a bit more what "don't work" means? Do you get an error? A blank page?
    ^ This. If you let us know what is specifically not working we can try to help you.

  4. #4
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there,

    I must say that saying that it doesn't work doesn't help much.

    What this is supposed to do is displaying all the jokes from the db table joke. If someone click on "add a joke", a form should be displayed and allow the user to add a joke. When submitted, the joke sould be added in the db and a message "Your joke has been added." should be displayed.

    The user should also be able to delete a joke when clicking on "Delete this joke".

    DB details (if you wanna a reproduce it):

    Connexion : 'localhost', 'root', 'root'
    DB name : ijdb
    Table name : joke
    +--+------------+-----------+
    | id | joke texte | jokedate |
    +--+------------+-----------+
    | 1 | this is the. | 09.08.25 |
    +--+------------+-----------+

    Just for you to know. This is not a job or anything like that. I am a complete newbie in php so I m just training and this is my very first exercice.

    Thanks :-)

    Thierry

    Thanks guys :-)

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    You still didn't explain what exactly isn't working. Do you get an error?

  6. #6
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Jokes are displayed but the form doesn't appear when clicking on "Add a joke" and jokes are not being delete when clicking on "Delete a joke"

    No error though

    Thanks

  7. #7
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    PHP Code:
    if (isset($_GET['deletjoke'])) { 
    Can you see the typo in this line?

    No idea why the form wouldn't show when you click on 'add a joke' though. What link do you see when you hover over it with your mouse?

  8. #8
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for your reply, I ll look at this tonight (it's 11:32 in SA) and get back to the forum asap.

    Thanks for your help, much appriciated.

  9. #9
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi im having the same issue, chapter 4

    I keep getting this error messege.


    Fatal error: Call to undefined function issets().....line 11

    <?php if(isset($_get['addjoke'])): ?

    can anyone help me.

    Thanks

  10. #10
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    The functions issets doesn't exist.

  11. #11
    SitePoint Guru r2d2's Avatar
    Join Date
    Dec 2003
    Location
    In my van, fool!
    Posts
    646
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I imagine the Add Joke link is not doing anything because you have

    PHP Code:
    if(isset($_get['addjoke'])) 
    near the top, and it needs to be

    PHP Code:
    if(isset($_GET['addjoke'])) 
    Note the $_GET, not $_get.

    You should have had an error displayed when trying to use $_get, so if you didn't, you should display errors while you are developing (but turn them off when you get to a live website - and just log them to file).

    I don't know what setup you are developing on, but in your php.ini file, look for a line with 'display_errors', and make sure it has 'display_errors = On' - it may be Off by default. You may also need to set 'error_reporting = E_ALL'.


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
  •