Page 1 of 1

AUTO-INCREMENT NOT WORKING

Posted: Sun May 04, 2008 2:50 pm
by mirra1515
I am trying to auto-increment my table and have had no success...any help would be spectacular.

My creation code is as follows:

Code: Select all

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}
 
mysql_select_db("test_db", $con);
$sql = "CREATE TABLE articleData 
(
article_id INT( 4 ) NOT NULL AUTO_INCREMENT,
articleTitle varchar(255),
articleDate varchar(255),
articleAuthor varchar(255),
articleSource varchar(255),
articleSubTitle varchar(255),
articleTeaser varchar(255),
articleContent longtext,
UNIQUE ( article_id )
)";
mysql_query($sql,$con);
 
mysql_close($con);
?>
Then my insertion code is as follows:

Code: Select all

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("test_db", $con);
 
$sql="INSERT INTO articleData (article_ID, articleTitle, articleSubtitle, articleAuthor, articleDate, articleSource, articleTeaser, articleContent)
VALUES
(NULL,'$_POST[articletitle]','$_POST[articlesubtitle]','$_POST[articleauthor]','$_POST[articledate]','$_POST[articlesource]','$_POST[articleteaser]','$_POST[articlecontent]')";
 
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
  
$URL="http://localhost/hr-bin/controlpanel/articles.php";
header ("Location: $URL");
 
mysql_close($con)
 
?>
My insertion code takes data from textboxes in another php file which appears to be working fine.

Whenever I simply view all of the rows, every column is filled in except the article_ID column (which remains blank).

Does anyone have any ideas as to what could be wrong with my code? :?:

Re: AUTO-INCREMENT NOT WORKING

Posted: Sun May 04, 2008 2:56 pm
by AXEmonster
why would you insert into the ID field when your wanting to auto increment

the database ID field should be setup to auto-increment which means you dont have to include that field in your insert statement

also your spelling is incorrect as you have _ID and _id but again if your using an auto-increment there is no need

Re: AUTO-INCREMENT NOT WORKING

Posted: Sun May 04, 2008 3:07 pm
by Spartan101
AXEmonster wrote:why would you insert into the ID field when your wanting to auto increment

the database ID field should be setup to auto-increment which means you dont have to include that field in your insert statement

also your spelling is incorrect as you have _ID and _id but again if your using an auto-increment there is no need
AXE is right. If you declare a field as NOT NULL then set it to NULL it's contradictory, you just simply don't need to declare it.

That should fix it for you.

So instead you would do the following:-

Code: Select all

$sql="INSERT INTO articleData (articleTitle, articleSubtitle, articleAuthor, articleDate, articleSource, articleTeaser, articleContent)
 VALUES ('$_POST[articletitle]','$_POST[articlesubtitle]','$_POST[articleauthor]','$_POST[articledate]','$_POST[articlesource]','$_POST[articleteaser]','$_POST[articlecontent]')";
Oh yeah, and pay attention to case sensitivity. It's better to make sure they always match for purposes of consistency and cleanliness of code even in non-case sensitive languages.

Re: AUTO-INCREMENT NOT WORKING

Posted: Sun May 04, 2008 11:21 pm
by mirra1515
That was exactly my problem, and here I was troubleshooting everything I could think of when I should have just checked my cases. Sorry for the stupid post :oops: , thank you for the responses!