Jump to content


Photo

My edit functions not working?


  • Please log in to reply
1 reply to this topic

#1 Perad

Perad
  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 28 October 2006 - 06:12 PM

Everything works apart from the final edit part. I keep getting "you cocked up" which the message when it fails. Could someone help me determine why this fails.

<php		
function editNews($id) {
				global $dbc;
				
				$query = "SELECT * FROM news WHERE id=$id";
				$result = mysql_query ($query);
				
				/* if we get no results back, error out */
				if (mysql_num_rows ($result) == 0) {
					echo "Invalid News ID\n";
					return;
				}
				$row = mysql_fetch_assoc($result);
				echo "<TABLE border=\"1\" width=\"580\">\n";
			
				/* easier to read variables and 
				 * striping out tags */
				$title = htmlentities ($row['title']);
				$news = nl2br (strip_tags ($row['newstext'], '<a><b><i><u>'));
				/* display the items */
				echo "<FORM action=\"{$_SERVER['PHP_SELF']}" . "?action=handleedit&id=$id\" method=POST>\n";
				echo "<input type=\"text\" name=\"newstitle\" value = \"".$title."\" size=\"50\" maxlength=\"50\" /><br />";
				echo "<TEXTAREA cols=\"60\" rows=\"12\" " . "name=\"content\">$news</TEXTAREA><BR>\n";
				echo "<input type=\"submit\" name=\"editsubmit\" " . "value=\"Save Changes\"\n";
				echo "</FORM>\n";
			}
			
			function handleEdit($id) {
				global $dbc;						
						$t = $_POST['newstitle'];
						$c = $_POST['content'];
						$query = "UPDATE news SET title='$t' newstext='$c' WHERE id='$id'" OR die ('Could not connect to MySQL: ' . mysql_error() );
						$result = mysql_query($query);
						if ($result) { // If it ran OK.
						
						// Confirmation.
						echo '<p><b>The news article has been added successfully!</b></p>';
						} else {
						echo 'You cocked it up';
						}
						exit(); // Quit the script.
											
						mysql_close(); // Close the database connection.
			}
			
			/* this is where the script decides what do do */
			if ($IsLoggedIn == 1) {	
			
					echo "<CENTER>\n";
					switch($_GET['action']) {
						case 'handleedit':
							handleEdit($_GET['id']);
							break;
						case 'edit':
							editNews($_GET['id']);
							break;
						case 'show':
							displayOneItem($_GET['id']);
							break;
						case 'all':
							displayNews(1);
							break;
						case 'addcomment':
							addComment($_GET['id']);
							break;
						default:
							displayNews();
					}
					
			}?>


#2 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 28 October 2006 - 06:57 PM

Try changing this:
$query = "UPDATE news SET title='$t' newstext='$c' WHERE id='$id'" OR die ('Could not connect to MySQL: ' . mysql_error() );
$result = mysql_query($query);

To this:
$query = "UPDATE news SET title='".$t."' newstext='".$c."' WHERE id='".$id."'";
$result = mysql_query($query) or die ('Could not connect to MySQL: ' . mysql_error() );

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users