James986 Posted November 29, 2006 Share Posted November 29, 2006 Hello,I have been programming in PHP probably no more than a weak. I am having trouble trying to add a record to a mySQL database.The code I use is below.[code]$blnSecurityCodeIsGood="";$blnSecurityCodeIsGood=false;if( isset($_POST['txtCAPTCHA']) && $_POST['txtCAPTCHA'] != $_SESSION['strCAPTCHACode'] ) { $blnSecurityCodeIsGood=false;} else { $blnSecurityCodeIsGood=true; $sql = "INSERT INTO 'tblBlogComments' ('CommentID', 'BlogID', 'UserID', 'Active', 'Username', 'Comment') VALUES (NULL, '1', '1', '1', 'James', 'test');";// if(!$sql) { echo mysql_error($dbQuery);// }; mysql_query($sql,$dbQuery); echo $sql."<br>Ok.";}[/code]As you can see i have tried to get it to display and SQL error message but no errors are displayed, but when i check the database through phpMyAdmin no modification have been made in any way, shape or form.Any help is wanted!Thanks in Advance.Regards,James Quote Link to comment Share on other sites More sharing options...
Jocka Posted November 29, 2006 Share Posted November 29, 2006 change the sql string to this:$sql = "INSERT INTO `tblBlogComments` ('CommentID', 'BlogID', 'UserID', 'Active', 'Username', 'Comment') VALUES (NULL, '1', '1', '1', 'James', 'test')";if CommentID is auto_increment however, don't use null. Use LAST_INSERT_ID() Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 29, 2006 Share Posted November 29, 2006 If that doesn't work, try this:$sql = "INSERT INTO `tblBlogComments` (`CommentID`, `BlogID`, `UserID`, `Active`, `Username`, `Comment`) VALUES (NULL, '1', '1', '1', 'James', 'test')"; Quote Link to comment Share on other sites More sharing options...
James986 Posted November 29, 2006 Author Share Posted November 29, 2006 Neither of them worked, CommentID is a primary key set to auto-incriment so i tried both queries with and without LAST_INSERT_ID().But nothing worked >:(Is there anything I missed out? If there an execute function you need to use once the query is specified? Quote Link to comment Share on other sites More sharing options...
bljepp69 Posted November 29, 2006 Share Posted November 29, 2006 Leave 'CommentID' out of your INSERT query:$sql = "INSERT INTO `tblBlogComments` (`BlogID`, `UserID`, `Active`, `Username`, `Comment`) VALUES ('1', '1', '1', 'James', 'test')";Since it's an auto_increment, it will increment without you doing anything to it. Every time you insert a row...increment. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.