Friday, March 16, 2012

MySQL auto-increment fail


I set the index of MySQL table as auto_increment. But auto_increment failed after "INSERT INTO".
Finally I found a solution.

The index of MySQL Table edu_course_cats is created using auto_increment:

DROP TABLE IF EXISTS `cesei_new1`.`edu_course_cats`;
CREATE TABLE  `cesei_new1`.`edu_course_cats` (
  `cat_id` mediumint(8) unsigned NOT NULL auto_increment,
  `cat_name` varchar(100) NOT NULL default '',
  `cat_parent` mediumint(8) unsigned NOT NULL default '0',
  `theme` varchar(30) NOT NULL default '',
  `dept_id` mediumint(8) unsigned NOT NULL default '0',
  `approved` tinyint(4) NOT NULL default '0',
  `member_id` mediumint(8) unsigned NOT NULL default '0',
  PRIMARY KEY  (`cat_id`)
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

In MySQL, when I used:
$sql = "INSERT INTO edu_course_cats VALUES ('', '$cat_name', $cat_parent_id, '$cat_theme', $dept_id, $approved, $mem_id)";
I got the error message : " Incorrect integer value"

There are two ways to fix this:
Method 1, using
$sql = "INSERT INTO edu_course_cats VALUES (NULL, '$cat_name', $cat_parent_id, '$cat_theme', $dept_id, $approved, $mem_id)";

Method 2, Set the MySQL mode not so strict
in my.ini in MYSQL server directory, line 87 change
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
to
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

Or use  MySQL workbench
Server  administration->Options File under configuration, select Advanced tab, check box
sql-mode, change
"STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
to
"NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

Then restart the MySQL server.
You may need to change php.ini permission to full control for your account via right click mouse on the file and select properties.

No comments:

Post a Comment