Jump to content

Add record to database ?!?


James986

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/28872-add-record-to-database/
Share on other sites

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()
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?
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.