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 ?


<!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 :slight_smile:

Thierry

I imagine the Add Joke link is not doing anything because you have

if(isset($_get['addjoke']))

near the top, and it needs to be

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’.

The functions issets doesn’t exist.

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

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. :eye:

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 :slight_smile:

Thierry

Thanks guys :slight_smile:

You still didn’t explain what exactly isn’t working. Do you get an error?

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

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?

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.