Jump to content

[SOLVED] Comments Insert and Display


Nexy

Recommended Posts

Why Hello There!  :) I seem to be having problems with my logic here:

 

<?php

$date = mysql_real_escape_string(stripslashes(date('m-j-Y')));
$subject = mysql_real_escape_string(stripslashes($_POST['ctitle']));
$comment = mysql_real_escape_string(stripslashes(nl2br($_POST['cidedit'])));
$id = $_GET['id'];

$nsql = "SELECT id, user, avatar, subject, date, news FROM news WHERE id = '".$_GET['id']."'";
$nres = mysql_query($csql) OR die(mysql_error());

if($row = mysql_fetch_array($nres)) {

echo "<div class='nstaff'>
       		<p class='avatar'>";

echo "<img src='";

	echo $row['avatar'];

echo "' alt='' class='tinyav' /></p>";
echo "<div class='nstaffcont'>";

	echo "<p class='title'>" . $row['subject'] . '</p>';
	echo "<p class='usern'>By: " . $row['user'] . '<br />';
	echo "Posted On: " . $row['date'] . '</p>';

	echo "<p class='news'>" . $row['news'] . '</p></div></div>';
}

$csql = "SELECT time, user, subject, content FROM comments WHERE newsid = '".$_GET['id']."'";
$cres = mysql_query($csql) OR die(mysql_error());

while($com = mysql_fetch_array($cres)) {

echo $com['subject'];
echo $com['time'];
echo $com['user'];
echo $com['content'];

}

if($_POST['cosub']) {

     if(!empty($subject) && !empty($comment)) {

if($_SESSION['username']) {
mysql_query("INSERT INTO comments(newsid, time, user, subject, content) VALUES ('".$_GET['id']."', '$date', '".$_SESSION['username']."', '$subject', '$comment')");
}
else if($_COOKIE['user']) {
mysql_query("INSERT INTO comments(newsid, time, user, subject, content) VALUES ('".$_GET['id']."', '$date', '".$_COOKIE['user']."', '$subject', '$comment')");
}

     }

     if(empty($subject) || empty($comment)) { 
echo "Error";
     }

}


if($_SESSION['username'] || $_COOKIE['user']) {

echo "<form action='#' method='post'>
         <fieldset id='com'>
         <legend>Add Comment:</legend>

         <p>
         <label for='ctitle'>Subject:</label>
         <input type='text' id='ctitle' name='ctitle' tabindex='6' /><br />
         <label for='cidedit'>Comment:</label><br />
         <textarea id='cidedit' name='cidedit' rows='6' cols='35' tabindex='7'></textarea>
                 </p>
         
         <input type='submit' id='cosub' name='cosub' value='Add Comment' tabindex='8' />
         <input type='reset' value='Clear Comment' tabindex='9' />

         </fieldset>
         </form>";
}
else if(!$_SESSION['username'] || !$_COOKIE['user']) { echo "Please login to post comments."; }

?>

 

The query right here:

$csql = "SELECT time, user, subject, content FROM comments WHERE newsid = '".$_GET['id']."'";

$cres = mysql_query($csql) OR die(mysql_error());

 

Keeps outputting, query is empty and stops the preceding and the code after it to stop displaying on the page. Shouldn't it just not show anything if it's empty?

 

If I was to take that part out, the is statement here:

if($_POST['cosub']) {

     if(!empty($subject) && !empty($comment)) {

if($_SESSION['username']) {
mysql_query("INSERT INTO comments(newsid, time, user, subject, content) VALUES ('".$_GET['id']."', '$date', '".$_SESSION['username']."', '$subject', '$comment')");
}
else if($_COOKIE['user']) {
mysql_query("INSERT INTO comments(newsid, time, user, subject, content) VALUES ('".$_GET['id']."', '$date', '".$_COOKIE['user']."', '$subject', '$comment')");
}

     }

     if(empty($subject) || empty($comment)) { 
echo "Error";
     }

}

 

It still inserts into the db even if subject or comment is empty. This is an included script, so session_start() and setting the cookie are before the <html>. Any help would be appreciated.

 

Thank You!  :)

Link to comment
Share on other sites

perhaps "empty" isn't catching it... try ...

 

if(!empty($subject) || if ($subject == "") && !empty($comment) || if ($comment == ""))

 

Try that in the appropriate locations and see if it makes a difference...

 

And I'm not sure about this though ... I think you need an "isset()" or "!isset()" ...

if($_SESSION['username'])

like ...

if(isset($_SESSION['username']))

Link to comment
Share on other sites

perhaps here...

newsid = '".$_GET['id']."'";

 

try this...

newsid = '{$_GET['id']}';

 

Whoops, was looking at wrong block of code... nevermind the stuff above ...

 

Try getting rid of the ... OR die(mysql_error());

$cres = mysql_query($csql) OR die(mysql_error());

 

and try this instead, see if it gives you any additional info...

 

if (!$cres) {
   $message  = 'Invalid query: ' . mysql_error() . "\n";
   $message .= 'Whole query: ' . $csql;
   die($message);
}

Link to comment
Share on other sites

Actually, I'd expect the error to be as a result of this:

 

$nsql = "SELECT id, user, avatar, subject, date, news FROM news WHERE id = '".$_GET['id']."'";
$nres = mysql_query($csql) OR die(mysql_error());

 

Notice that you define $nsql as the querystring and then use $csql when you execute the query ... $csql has no value!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.