kingbear,
Thanks. Actually the table is called 'contents' and there in an integer field called 'blogID' already in there. I tried the code you posted and here's what is happening.
If I do it exactly like you have it without the ' around $blog towards the end of the query I get this error.
Error in query: INSERT INTO comments(commentUSER, commentENTRY, commentTITLE, blogID) VALUES('Test', 'TEst', 'Test', ). 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
If I put a ' around the $blog it inserts the info to the database, but sets the blogID field to 0.
Here's my full code:
<?
// form not yet submitted
// display initial form
if (!$submit)
{
?>
<table cellspacing="5" cellpadding="5">
<form action="<? echo $PHP_SELF; ?>" method="POST" name="theform" id="theform">
<tr>
<td width="100" align="right"><font class="text-blog"><b>Name: </b></font></td>
<td align="left"><input size="40" maxlength="250" type="text" name="commentUSER"></td>
</tr>
<tr>
<td width="100" align="right"><font class="text-blog"><b>Title: </b></font></td>
<td align="left"><input size="40" maxlength="250" type="text" name="commentTITLE"></td>
</tr>
<tr>
<td width="100" align="right" valign="top"><font class="text-blog"><b>Comment: </b></font></td>
<td align="left"><textarea name="commentENTRY" cols="40" rows="10"></textarea></td>
</tr>
<tr>
<td colspan=2 align="center"><input type="Submit" name="submit" value="Add"></td>
</tr>
</form>
</table>
<?
}
else
{
// includes
include("dbconnex.php");
// set up error list array
$errorList = array();
$count = 0;
// validate text input fields
if (!$commentTITLE) { $errorList[$count] = "Invalid entry: Please enter your name"; $count++; }
// check for errors
// if none found...
if (sizeof($errorList) == 0)
{
// open database connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// generate and execute query
$blogID = $_GET["blogID"];
$query = "INSERT INTO comments(commentUSER, commentENTRY, commentTITLE, blogID) VALUES('$commentUSER', '$commentENTRY', '$commentTITLE', $blogID)";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
// print result
echo "<font class='text-blog'>Your comment was succesfully added. </font><a href='../index.php'>Go back</a>.</font>";
// close database connection
mysql_close($connection);
}
else
{
// errors found
// print as list
echo "<font size=-1>The following errors were encountered: <br>";
echo "<ul>";
for ($x=0; $x<sizeof($errorList); $x++)
{
echo "<li>$errorList[$x]";
}
echo "</ul></font>";
}
}
?>