Page 1 of 1

[SOLVED]comment form inserting record when null

Posted: Tue Mar 22, 2005 2:52 am
by pleigh
i have this comment form

Code: Select all

<!--comment form-->
		<form action="<? $_SERVER['PHP_SELF']; ?>" method="post">
		<table>
		<?
		if (isset($_POST['submit']))
		{
			$message = NULL;
			
			if (empty($_POST['comment']))
			{
				$c = FALSE;
				$message .= 'Please enter your comment!<br>';
			}
			else
			{
				$c = addslashes($_POST['comment']);
			}
			
			$updatequery = "UPDATE posts SET postupdate=NOW() WHERE postID='$id'";
			$updateresult = @mysql_query($updatequery);
				
			$query = "INSERT INTO comments(comment, postID, username, commentdate)
			VALUES('$c','$_GET[pid]','$fn', NOW())";
			$result = @mysql_query($query);
			
			if ($updatequery && $result)
			{
					
				echo '<b/>Your comment has been posted!';
				echo '<meta http-equiv="refresh" content="3;url=http://localhost/mysample/report.php">';
				exit();
				//}
			}
			else
			{
				echo 'Your comment cannot be posted due to system error!'.mysql_error();
			}
			//mysql_close();
		}
		
		if (isset($message))
		{
			echo '<font color="red">', $message, '</font>';
		}
		?>
		<tr valign="top">
		  	<td width="20%" align="right"><b>COMMENT</b></td>
			<td width="1%"><b>:</b></td>
			<td width="79%"><textarea name="comment"  style="width:350px"cols="40" rows="6"></textarea></td>
		  </tr>
		  
		  <tr valign="top">
		  	<td width="20%"></td>
			<td width="1%"></td>
			<td width="79%"><input type="submit" name="submit" value="submit" class="buttonformat"></td>
		  </tr>
		</table>
		</form>
when the textarea is blank, then i click the submit button, it still records the blank data in the database....pls help

tnx....

Posted: Tue Mar 22, 2005 3:11 am
by n00b Saibot
Simply inspect this snippet from your code

Code: Select all

if (empty($_POST['comment']))
            {
                $c = FALSE;
                $message .= 'Please enter your comment!<br>';
            }
            else
            {
                $c = addslashes($_POST['comment']);
            }
you set $c to FALSE if the comment is blank. then you insert it in the DB

Code: Select all

$query = "INSERT INTO comments(comment, postID, username, commentdate) VALUES('$c','$_GET[pid]','$fn', NOW())";
instead of inserting the $message that you set it to. :?


feyd | :grumble:

Posted: Tue Mar 22, 2005 3:27 am
by pleigh
yes noob...tnx...i forgot to include this line of code

Code: Select all

if ($c)
{
     ....
}
before the queries...