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
Thierry