Quote
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Now here's the code for the shownews.php file which is being brought in as the index content.
<?php
include('dbconnect.php');
include('check.php');
$sql = mysql_query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT 10") or die(mysql_error());
while ($results = mysql_fetch_array($sql)) {
if (mysql_num_rows($sql) == 0) {
echo("Sorry, no news at the moment. Check back later!");
}
else {
$commentcheck = mysql_query("SELECT * FROM `newscomments` WHERE newsid=$id") or die(mysql_error());
$comments = mysql_num_rows($commentcheck);
$id = $results['id'];
$title = $results['title'];
$user = $results['user'];
$date = $results['date'];
$news = $results['message'];
echo ("<div class='newstop'><a href='./?id=news&newsid=$id' style='font-weight:bold; '>$title</a> posted by <a href='./?id=member&user=$user' style='font-weight:bold; font-style:italic; '>$user</a> ($date) - $comments comment(s)</div><br />\n$news");
}
}
?>
And the news.php to view seperate news items which then includes the newscomments.php file
news.php
<?php
include('check.php');
include('dbconnect.php');
$id = $_GET['newsid'];
$sql = mysql_query("SELECT * FROM `news` WHERE id='$id'");
while ($results = mysql_fetch_array($sql)) {
if (mysql_num_rows($sql) == 0) {
echo("Sorry, no news matches the ID in the address bar. Make sure that you've got the link right!");
}
else {
$title = $results['title'];
$user = $results['user'];
$date = $results['date'];
$news = $results['message'];
echo ("<div class='newstop'><strong>$title</strong> posted by <a href='./?id=member&user=$user' style='font-weight:bold; font-style:italic; '>$user</a> ($date)</div><br />\n$news<br /><hr />");
include("newscomments.php");
}
}
?>
newscomments.php
<?php
include('check.php');
include('dbconnect.php');
$newsid = $_GET['newsid'];
$commentsearch = mysql_query("SELECT * FROM `newscomments` WHERE newsid='$newsid' ORDER BY id DESC");
while($comments = mysql_fetch_array($commentsearch)) {
if(mysql_num_rows($commentsearch) == 0) {
echo("There are no comments on this news item. Be the first to post.");
}
else {
$poster = $comments['user'];
$date = $comments['date'];
$post = $comments['message'];
echo("<div class='newscomments'>Posted by <a href='./?id=member&user=$poster' style='font-weight:bold; font-style:italic; '>$poster</a> ($date)</div><br />$post");
}
}
?>
<?php
$user = $_COOKIE['user'];
$check = mysql_query("SELECT * FROM `users` WHERE level='1' OR level='2' OR level='3' OR level='4' or level='5' AND username='$user'");
if (mysql_num_rows($check) == 0) {
echo ("You must be logged in to add comments to a news item.<br />");
}
else {
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" >
<table width="100%">
<tr>
<td width="27%">Your Comment</td>
<td colspan="2"><textarea rows="6" cols="20" style="margin:2px; " name="comment"></textarea></td>
</tr>
<tr>
<td width="27%">Security code:
<br />
<em>This is to stop spam</em></td>
<td width="25%" align="left" valign="middle">
<input class="text" name="secCode" type="text" size="12" style="margin:2px; " /></td>
<td width="48%" align="left" valign="middle"><img src="captcha.php" alt="security code" border="1" style="margin:2px; " /></td>
</tr>
<tr>
<td colspan="3" align="center"><br/>
<input class="text" type="submit" name="submit" value="Add Comment" />
</td>
</tr>
</table>
</form>
<?php
if (isset($_POST['submitBtn'])){
$secCode = isset($_POST['secCode']) ? strtolower($_POST['secCode']) : "";
if ($secCode == $_SESSION['securityCode']) {
echo "<p>The result code was valid!<br/></p>";
unset($_SESSION['securityCode']);
$result = true;
}
else {
echo "<p>Sorry the security code is invalid! Please try it again!</p>";
$result = false;
}
}
if (isset($_POST['submit']) && $result = true) {
$usercomment = $_POST['comment'];
$date = date("Y-m-d");
mysql_query("INSERT INTO `newscomments` SET newsid='$newsid', message='$usercomment', date='$date', user='$user'") or die(mysql_error());
echo("Comment added!");
}
}
?>
And the second problem I have is with the newscomments.php file. When the user isn't logged in, they can't view the comments. What I'm after is for a user not logged in can view the news comments but not post.
It's been a while since I've done any PHP coding because I stopped to concentrate on my GCSEs. But they're done now so I'm back working on some old projects.
Can anyone help?
