AUTO-INCREMENT NOT WORKING

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mirra1515
Forum Commoner
Posts: 29
Joined: Fri Apr 25, 2008 3:17 am

AUTO-INCREMENT NOT WORKING

Post 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? :?:
AXEmonster
Forum Commoner
Posts: 34
Joined: Fri Sep 05, 2003 11:27 am
Location: newcastle UK

Re: AUTO-INCREMENT NOT WORKING

Post 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
Spartan101
Forum Newbie
Posts: 12
Joined: Mon Feb 25, 2008 4:56 pm
Location: Manchester (UK)

Re: AUTO-INCREMENT NOT WORKING

Post 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.
User avatar
mirra1515
Forum Commoner
Posts: 29
Joined: Fri Apr 25, 2008 3:17 am

Re: AUTO-INCREMENT NOT WORKING

Post 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!
Post Reply