Hi all. I've written a script that takes news items from a database and then prints it, along with the comments associated with it. This works OK. I then have a form at the bottom of the page so that someone can add a new comment. My code(comments.php is linked to using comments.php?ID=):
$Connect = odbc_pconnect("Cy6", "Cy6", "indigo");
$Query = "SELECT * FROM News WHERE ID = $ID";
$Result = odbc_exec($Connect, $Query);
//Print news article
while(odbc_fetch_row($Result))
{
$NAuthor = stripslashes(odbc_result($Result, 1));
$NPostDate = odbc_result($Result, 2);
$NTitle = stripslashes(odbc_result($Result, 3));
$NPost = stripslashes(odbc_result($Result, 4));
$NID = stripslashes(odbc_result($Result, 5));
}
print("<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\" width=\"100%\" bordercolor=\"#000099\">\n");
print("<tr>\n");
print("<th align=\"left\">$NTitle</th>\n");
print("</tr>\n");
print("<tr>\n");
print("<td>$NPost\n");
print("<p>[Posted by $NAuthor on $NPostDate]\n");
print("</td>\n");
print("</tr>\n");
print("</table>\n");
print("<br>\n");
//Print comments associated with news article
$Cquery = "SELECT * FROM Comments WHERE NewsID = $NID ORDER BY PostDate DESC";
$Cresult = odbc_exec($Connect, $Cquery);
while(odbc_fetch_row($Cresult))
{
$CID = stripslashes(odbc_result($Cresult, 1));
$CAuthor = stripslashes(odbc_result($Cresult, 3));
$CPostDate = odbc_result($Cresult, 4);
$CTitle = stripslashes(odbc_result($Cresult, 5));
$CPost = stripslashes(odbc_result($Cresult, 6));
print("<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\" width=\"100%\" bordercolor=\"#000099\">\n");
print("<tr>\n");
print("<th align=\"left\">$CTitle</th>\n");
print("</tr>\n");
print("<tr>\n");
print("<td>$CPost\n");
print("<p>[Posted by $CAuthor on $CPostDate]\n");
print("</td>\n");
print("</tr>\n");
print("</table>\n");
print("<br>\n");
}
//Print form to add a new comment
print("<form action=\"addcomment.php\" method=\"POST\">\n");
print("<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\" bordercolor=\"#000099\">\n");
print("<tr>\n");
print("<td>Author:</td>\n");
print("<td><input type=\"text\" name=\"Author\"></td>\n");
print("</tr>\n");
print("<tr>\n");
print("<td>Header:</td>\n");
print("<td><input type=\"text\" name=\"Header\"></td>\n");
print("</tr>\n");
print("<tr>\n");
print("<td>Comment:</td>\n");
print("<td><textarea rows=\"7\" name=\"Comment\" cols=\"32\"></textarea></td>\n");
print("</tr>\n");
print("</table>\n");
print("<p>\n");
print("<input type=\"Submit\" name=\"Submit\" value=\"Post comment\">\n");
$Author = str_replace("'", "''", $Author);
$Header = str_replace("'", "''", $Header);
$Comment = str_replace("'", "''", $Comment);
$NewsID = $ID;
and then addcomments.php:
if($Submit)
{
$Connect = odbc_connect("Cy6", "Cy6", "indigo");
$newComment = "INSERT INTO Comments(Author, NewsID, PostDate, Title, Comment) VALUES('$Author', $NewsID, now(), '$Header', '$Comment')";
odbc_exec($Connect, $newComment);
}
The problem is that the NewsID variable is being left empty and when the submit button is pressed, an SQL syntax error is generated 🙁. If I remove NewsID and $NewsID from my SQL statement, it works, just the NewsID field is left empty. The NewsID field cannot be left empty so that when comments.php runs, it can pick out the comments for a particular news item. Any ideas?
Any help appreciated, thanx.