Jump to content

mysql table creation errors


ryanhowdy

Recommended Posts

I can't figure out what's wrong with the following code.  It seems to work fine on some servers and not on others, I think it is a MySQL 5 problem, but I'm not sure.

 

mysql_query("CREATE TABLE `fcms_address` (
`id` int(11) NOT NULL auto_increment, 
`user` int(11) NOT NULL default '0', 
`entered_by` INT(11) NOT NULL DEFAULT '0', 
`updated` timestamp NOT NULL default CURRENT_TIMESTAMP, 
`address` varchar(50) default NULL, 
`city` varchar(50) default NULL, 
`state` varchar(50) default NULL, 
`zip` varchar(10) default NULL, 
`home` varchar(20) default NULL, 
`work` varchar(20) default NULL, 
`cell` varchar(20) default NULL, 
PRIMARY KEY  (`id`), 
KEY `user_ind` (`user`), 
KEY `ent_ind` (`entered_by`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1") or die(mysql_error());
mysql_query("ALTER TABLE `fcms_address` 
ADD CONSTRAINT `fcms_address_ibfk_1` 
FOREIGN KEY (`user`) REFERENCES `fcms_users` (`id`) ON DELETE CASCADE"
) or die(mysql_error());
mysql_query("ALTER TABLE `fcms_address` 
ADD CONSTRAINT `fcms_address_ibfk_2` 
FOREIGN KEY (`entered_by`) REFERENCES `fcms_users` (`id`) ON DELETE CASCADE"
) or die(mysql_error());

 

And I get the following error:

Cannot add or update a child row: a foreign key constraint fails

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/107662-mysql-table-creation-errors/
Share on other sites

I can't figure out what's wrong with the following code.  It seems to work fine on some servers and not on others, I think it is a MySQL 5 problem, but I'm not sure.

 

mysql_query("CREATE TABLE `fcms_address` (
`id` int(11) NOT NULL auto_increment, 
`user` int(11) NOT NULL default '0', 
`entered_by` INT(11) NOT NULL DEFAULT '0', 
`updated` timestamp NOT NULL default CURRENT_TIMESTAMP, 
`address` varchar(50) default NULL, 
`city` varchar(50) default NULL, 
`state` varchar(50) default NULL, 
`zip` varchar(10) default NULL, 
`home` varchar(20) default NULL, 
`work` varchar(20) default NULL, 
`cell` varchar(20) default NULL, 
PRIMARY KEY  (`id`), 
KEY `user_ind` (`user`), 
KEY `ent_ind` (`entered_by`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1") or die(mysql_error());
mysql_query("ALTER TABLE `fcms_address` 
ADD CONSTRAINT `fcms_address_ibfk_1` 
FOREIGN KEY (`user`) REFERENCES `fcms_users` (`id`) ON DELETE CASCADE"
) or die(mysql_error());
mysql_query("ALTER TABLE `fcms_address` 
ADD CONSTRAINT `fcms_address_ibfk_2` 
FOREIGN KEY (`entered_by`) REFERENCES `fcms_users` (`id`) ON DELETE CASCADE"
) or die(mysql_error());

 

And I get the following error:

Cannot add or update a child row: a foreign key constraint fails

 

Any ideas?

 

you dont close the parenthesis in the first line?

Archived

This topic is now archived and is closed to further replies.

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