Part of the EllisLab Network
   
 
sql error. How to add autoincrement id to the INSERT
Posted: 26 February 2009 12:59 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  35
Joined  02-21-2009

I followed by this IBM article http://www.ibm.com/developerworks/opensource/library/wa-codeigniter/index.html
but i receive this answer from the mysql database

Error Number: 1062

Duplicate entry
'0' for key 1

INSERT INTO
`guestbook` (`name`, `email`, `text`, `ipaddress`, `date`) VALUES ('assa', 'Alex', 'alex@email.com', '127.0.0.1', '2009-02-26 08:53:35')

here is a database structure

CREATE TABLE `guestbook` (
  `
id` int(11) NOT NULL auto_increment,
  `
name` varchar(128) NOT NULL,
  `
email` varchar(255) NOT NULL,
  `
text` text NOT NULL,
  `
date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `
ipaddress` varchar(32) NOT NULL,
  
PRIMARY KEY  (`id`)
)
ENGINE=MyISAM;

this is Insert function:

function addContact(){
  $now
= date("Y-m-d H:i:s");
  
$data = array(
    
'name' => $this->input->xss_clean($this->input->post('name')),
    
'email' => $this->input->xss_clean($this->input->post('email')),
    
'text' => $this->input->xss_clean($this->input->post('notes')),
    
'ipaddress' => $this->input->ip_address(),
    
'date' => $now
  
);

  
$this->db->insert('guestbook', $data);
}

if i use id in this data array it will insert all data succsesfully, but I don’t want to care about id number - it is primary auto_increment key. How to solve problem?

Profile
 
 
Posted: 26 February 2009 03:34 AM   [ Ignore ]   [ # 1 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1095
Joined  05-17-2008

Try the following:

$data = array(
    
'id' => '',
    
'name' => $this->input->xss_clean($this->input->post('name')),
    
'email' => $this->input->xss_clean($this->input->post('email')),
    
'text' => $this->input->xss_clean($this->input->post('notes')),
    
'ipaddress' => $this->input->ip_address(),
    
'date' => $now
  
);
Profile
 
 
Posted: 26 February 2009 03:47 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Rank
Total Posts:  35
Joined  02-21-2009
pistolPete - 26 February 2009 03:34 AM

Try the following:

$data = array(
    
'id' => '',
    
'name' => $this->input->xss_clean($this->input->post('name')),
    
'email' => $this->input->xss_clean($this->input->post('email')),
    
'text' => $this->input->xss_clean($this->input->post('notes')),
    
'ipaddress' => $this->input->ip_address(),
    
'date' => $now
  
);

nothing happend - the same error I recieved.

on another forum on man recommend me to make this one

SHOW TABLE STATUS LIKE 'guestbook'

and that command says that my db corrupted. What I should do now?

Profile
 
 
Posted: 26 February 2009 03:50 AM   [ Ignore ]   [ # 3 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1095
Joined  05-17-2008
linderox - 26 February 2009 03:47 AM

What I should do now?

How about repairing the database?

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120509 Total Logged-in Users: 34
Total Topics: 126576 Total Anonymous Users: 4
Total Replies: 665465 Total Guests: 368
Total Posts: 792041    
Members ( View Memberlist )