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
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()
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.