ryanhowdy Posted May 28, 2008 Share Posted May 28, 2008 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 More sharing options...
ILYAS415 Posted May 28, 2008 Share Posted May 28, 2008 Yes I think its because mysql 5 and above only allow foreign keys. Anything below will fail. Only advice i can give is to upgrade the server your using or change your query. Imnot 100% tahts the problem though. More like 70%? Link to comment https://forums.phpfreaks.com/topic/107662-mysql-table-creation-errors/#findComment-551974 Share on other sites More sharing options...
prcollin Posted May 28, 2008 Share Posted May 28, 2008 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? Link to comment https://forums.phpfreaks.com/topic/107662-mysql-table-creation-errors/#findComment-551992 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.