Problem with comment box and mysql

Hi guys. I am a beginner in PHP and I was trying to make a comment box based on a login system the other day.

The problem is with the comment page. Looks like the MySQL database does not receive the data.
Here’s the full code:
[php]

<?php echo ""; echo ""; include ("login.php"); echo "
"; echo "

Comment here:

"; echo ""; echo "subject:

comment:

"; echo ""; echo ""; function is_login() { if (isset($_COOKIE['username']) AND isset($_COOKIE['password'])) return true; else return false; } $is_login = is_login(); if ($is_login == 'true' AND isset($_POST['submit_comment'])) { $subject = $_POST['subject']; $comment = $_POST['comment']; connect_sql(); $connect = connect_sql(); if (!$connect) die ("Could not connect: " . mysql_error()); else { $subject = htmlspecialchars($subject); $comment = htmlspecialchars($comment); $name = $_COOKIE['username']; $str_s = strlen($subject); $str_c = strlen($comment); if (empty($subject) OR empty($comment)) echo "Please write down subject and comment"; elseif ($str_s < 2 OR $str_s > 25) echo "Subject must be 2 to 25 characters long."; elseif ($str_c < 2 OR $str_c > 200) echo "Subject must be 2 to 200 characters long."; else { mysql_query ("INSERT INTO commentlist (name, subject, comment) VALUES ('$name', '$subject', $comment')"); echo "Comment has been submitted. You can refresh the webpage to see your comment. If your comment is inappropriate, we'll make sure to delete it."; } } } elseif ($is_login == 'false' AND isset($_POST['submit_comment'])) { echo "You have to be logged in to comment. Please log in or register if you don't have an account."; } $connect = connect_sql(); if ($connect) { $result = mysql_query ("SELECT * FROM commentlist"); if (isset($result)) { $row = mysql_fetch_array($result); if (isset($row[0])) { echo "
by " . $row['name']; echo "" . $row['subject'] . "
"; echo "

" . $row['comment'] . "

"; echo "

"; } } mysql_close(); } echo ""; ?>

[/php]

What is the problem?

You’re INSERT query is missing a semi-colon at the end.
[php]mysql_query (“INSERT INTO commentlist (name, subject, comment) VALUES (’$name’, ‘$subject’, $comment’)”);[/php]

Should be
[php]
mysql_query (“INSERT INTO commentlist (name, subject, comment) VALUES (’{$name}’, ‘{$subject}’, ‘{$comment}’);”);
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service