Build your own database driven website using php & mysql (3rd edition)

i hope someone can help. When i try to delete a joke from the database the joke does not get deleted. I do not know whats wrong. If anyone can help I would appreciate it. Thanks in advance. I will paste the code here.


<html>
<head>
<title>The Internet Joke Database<title/>
</head>
<body>

<?php if (isset($_GET['addjoke'])): // User wants to add a joke
?>

<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 Database
$dbcnx=@mysql_connect('******','*****','*****');
$dbname='****';
if (!dbcnx) {
	echo '<p>Unable to connect to the ' . 'database server at this time. </p>';
	exit();
}
else
echo '<p>Connection established!</p>';


mysql_select_db('ddd19', $dbcnx);
if(!@mysql_select_db('ddd19')) {
exit('<p>Unable to locate the joke ' . 'database at this time. </p>');
}
else 
echo '<p>Connection to database established</p>';

//if a joke has been submitted, 
//add it to the database. 
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 a joke has been deleted,
// remove it from the database.
if (isset($_GET['deletejoke'])) {
$jokeid = $_GET['deletejoke'];
$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 text of all the jokes
$result = @mysql_query('SELECT joketext FROM joke');
if (!$result) {
exit('<p>Error performing query: ' .
mysql_error() . '</p>');
}
// Display the text of each joke in a paragraph
//with a "Delete this joke" link next to each.
while ($row = mysql_fetch_array($result)) {
	$jokeid = $row['id'];
	$joketext = $row['joketext'];
echo '<p>' . $joketext . '<a href="' . $_SERVER['PHP_SELF'] . '?deletejoke=' . $jokeid . '">' . 'Delete this joke</a></p>';
}
// When clicked, this link will load this page
// with the joke submission form displayed.
echo '<p><a href="' . $_SERVER['PHP_SELF'] .
'?addjoke=1">Add a Joke!</a></p>';
endif;
?>


</body>
</html>

ok, you just need to do some basic debugging

  1. start at the top of the script as specified in your form’s action attribute and add
 
echo 'got here'; die();

  1. run your form to check if it gets to your php script

  2. then move the above echo/die down, line by line if you have to, and add appropriate echo statements to display values of variables and then run the form again each time you move the echos.

  3. as part of 3) insert the echos in each part of conditional blocks (IF blocks) to check your code logic is correct

keep doing this until your echos show something is not right. then back track your code to fix the error.

  1. keep repeating 3) and 4) until you get to the end of your script and it works ok.

if you have a debugger, then debugguing will be easier as you can set break points and check values of variables which is essentially what the above steps are doing.

ok i will try that

ok i got it. I forgot to add the id in the select query :slight_smile:

thanks for the debugging tip. that helped out alot.

my mistake

thank you