Jump to content
Larry Ullman's Book Forums

Chapter 12 - Edit Blog Entry

Recommended Posts

Hi all, I'm having a problem editing the database whilst running the edit_entry.php script p.373.


I'll include my script, apologises if it code bloat!


Just to add I can, add, view and delete records in my database fine.




if ( isset($_GET['id']) && is_numeric($_GET['id']) ) {

$query = 'SELECT title, entry FROM entries WHERE entry_id = ' . $_GET['id'] . '';

if ( $result = mysql_query($query, $dbc)) {

	$row = mysql_fetch_array($result);

	print '
	<form action="edit_entry.php" method="post">
	Entry Title: <input type="text" name="title" value="' . $row['title'] . '" />
	Entry Text: <textarea name="entry" rows="5" cols="40">' . $row['entry'] . '</textarea>
	<input type="hidden" name="id" value="' . $_GET['id'] . '" />
	<input type="submit" name="submit" value="Edit entry" />

} else {

	print 'The entry could not be accessed. The query being ran is ' . $query . '';


} elseif ( isset($_POST['id']) && is_numeric($_POST['id']) ) {

$problem = FALSE;

if ( !empty($_POST['title']) && !empty($_POST['entry']) ) {

	$title = $_POST['title'];
	$title = trim(strip_tags($title));
	$title = mysql_real_escape_string($title, $dbc);

	$entry = $_POST['entry'];
	$entry = trim(strip_tags($entry));
	$entry = mysql_real_escape_string($entry, $dbc);

} else {

	$problem = TRUE;
	print '<p class="error">Please enter a title and entry</p>';


if (!$problem) {

	$query = 'UPDATE entries SET title = ' . $title . ', entry = ' . $entry . ' WHERE entry_id = ' . $_POST['id'] . ' LIMIT 1';

	$result = mysql_query($query, $dbc);

	if (mysql_affected_rows($dbc) == 1) {

		print '<p>The entry has been updated</p>';

	} else {

		print '<p class="error">The entry could not be editied. The query being run is: ' . $query . '</p>';


} else {

print '<p class="error">This page was accessed in error</p>';





After running the above script I get the error


The entry could not be editied. The query being run is: UPDATE entries SET title = Number 9, entry = Blaa Blaa WHERE entry_id = 11 LIMIT 1

Link to comment
Share on other sites

Ran a mysql_error and it turns out my query had an error.


$query = 'UPDATE entries SET title = ' . $title . ', entry = ' . $entry . ' WHERE entry_id = ' . $_POST['id'] . ' LIMIT 1';


I used this instead (from the book) and it worked:


$query = "UPDATE entries SET title='$title', entry='$entry' WHERE entry_id='{$_POST['id']}' LIMIT 1";


My question would now be how could I have got this to work using single quotes on my query?


N.B The reason I didn't use the book code in the first place is I try to hand code it myself without reference for learning purposes.

Link to comment
Share on other sites

  • 1 year later...

is there someone there tried not to change the retrieved title and entry and hit update button and get an error?


when i run the code and did some changes it goes perfectly but when i did not make any change to the retrieved title and entry it says an error. the thing is that when i make some changes it works but when i choose not to make any changes and hit "update this entry" it says an error. isn't that the problem is you have too make an actual change? what i mean is that once i click edit i should change it atleast one letter or put space so i can update it? what if i decided not to change anything and click update?

Link to comment
Share on other sites

mysql returns an error on an update query if nothing has been updated. How this scenario is handled for the user is a developer decision. You can trap the error and handle it in a user friendly way or you can give the user an opt out button.



larry's code in chapter 13 in section under edit. it works perfectly even i dont change the retrieved information at all. my concern is in chapter 12 it cannot done the same. it seems like there is something on larry's chapter'12 code an error? but if i edit it it works perfectly. argh! i'm stuck! in chapter 12.

Link to comment
Share on other sites


  • Create New...