Cdbcommand Failed To Execute The Sql Statement: Sqlstate[23000]: Integrity Constraint Violation: 1452 Cannot Add Or Update A Child Row: A Foreign Key

i want to make an application in my project i.e, "create users comments" in my project ,i have found comment form in my page but when i enter data in the form and submit that i have found an error that is…

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (technoc1_notjustprice.tbl_comment, CONSTRAINT FK_comment_product FOREIGN KEY (product_id) REFERENCES tbl_product_index (product_id) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO tbl_comment (content, status, create_time, author, email, url) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5)

in this time i am not able to find the error please help me for finding the error…this error occurs due to database table relations…here the relation between user,product and comments is…

1 user give many comments and

1 product have many comments.

all my files are in below…

--------"tables"------

– Table structure for table tbl_comment

CREATE TABLE IF NOT EXISTS tbl_comment (

id int(11) NOT NULL AUTO_INCREMENT,

content text NOT NULL,

status int(11) NOT NULL,

create_time varchar(50) NOT NULL,

author varchar(50) NOT NULL,

email varchar(50) NOT NULL,

url varbinary(50) NOT NULL,

product_id varchar(30) NOT NULL,

PRIMARY KEY (id),

KEY product_id (product_id)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=37 ;

– Dumping data for table tbl_comment

INSERT INTO tbl_comment (id, content, status, create_time, author, email, url, product_id) VALUES

(11, ‘fhwfh’, 1, ‘00:00:00’, ‘raj’, ‘abc@gmail.com’, ‘www.ffj.in’, ‘mxn10’),

(18, ‘gui2’, 1, ‘00:00:00’, ‘raja’, ‘fi;u’, ‘fb2of’, ‘mxn’),

(19, ‘gui2’, 1, ‘00:00:00’, ‘raja’, ‘fi;u’, ‘fb2of’, ‘mxn’),

(23, ‘;vibw’, 2, ‘bvhikv’, ‘dj.vh qf q’, ‘hvjqq’, ‘ikq.v’, ‘mxn10’),

(24, ‘;vibw’, 2, ‘bvhikv’, ‘dj.vh qf q’, ‘hvjqq’, ‘ikq.v’, ‘mxn10’),

(25, 'jjlfwbj ', 3, ‘00:01:02’, ‘e3gA’, ‘FWG’, ‘F3W’, ‘mxn’);

– Constraints for dumped tables

– Constraints for table tbl_comment

ALTER TABLE tbl_comment

ADD CONSTRAINT FK_comment_product FOREIGN KEY (product_id) REFERENCES tbl_product_index (product_id) ON DELETE CASCADE;

– Table structure for table tbl_product_index

CREATE TABLE IF NOT EXISTS tbl_product_index (

index int(11) NOT NULL AUTO_INCREMENT,

product_name varchar(30) NOT NULL,

product_id varchar(30) NOT NULL,

master_key int(11) NOT NULL,

PRIMARY KEY (product_id),

UNIQUE KEY master_key (master_key),

KEY index (index)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

– Dumping data for table tbl_product_index

INSERT INTO tbl_product_index (index, product_name, product_id, master_key) VALUES

(1, ‘micromax A100’, ‘mxn’, 1),

(2, ‘micromax A10’, ‘mxn10’, 2);

– Constraints for dumped tables

– Constraints for table tbl_product_index

ALTER TABLE tbl_product_index

ADD CONSTRAINT tbl_product_index_ibfk_1 FOREIGN KEY (master_key) REFERENCES tbl_master_index (master_key) ON DELETE CASCADE;

– Table structure for table tbl_users

CREATE TABLE IF NOT EXISTS tbl_users (

id int(10) unsigned NOT NULL AUTO_INCREMENT,

username varchar(20) NOT NULL,

password varchar(128) NOT NULL,

activationKey varchar(128) NOT NULL DEFAULT ‘’,

createtime int(10) NOT NULL DEFAULT ‘0’,

lastvisit int(10) NOT NULL DEFAULT ‘0’,

lastaction int(10) NOT NULL DEFAULT ‘0’,

lastpasswordchange int(10) NOT NULL DEFAULT ‘0’,

superuser int(1) NOT NULL DEFAULT ‘0’,

status int(1) NOT NULL DEFAULT ‘0’,

avatar varchar(255) DEFAULT NULL,

notifyType enum(‘None’,‘Digest’,‘Instant’,‘Threshold’) DEFAULT ‘Instant’,

PRIMARY KEY (id),

UNIQUE KEY username (username),

KEY status (status),

KEY superuser (superuser)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

– Dumping data for table tbl_users

INSERT INTO tbl_users (id, username, password, activationKey, createtime, lastvisit, lastaction, lastpasswordchange, superuser, status, avatar, notifyType) VALUES

(1, ‘admin’, ‘21232f297a57a5a743894a0e4a801fc3’, ‘’, 1350028839, 1350032069, 0, 0, 1, 1, NULL, ‘Instant’),

(2, ‘demo’, ‘fe01ce2a7fbac8fafaed7c982a04e229’, ‘’, 1350028839, 0, 0, 0, 0, 1, NULL, ‘Instant’);

thanks in advance…

Hi, your problem is in tbl_comment. If you look at the executed request ( INSERT INTO tbl_comment …), you can see that the field product_id is not set.

You should check the model file for tbl_comment.

thanks a lot ragua…

below file is comment model…

here when i see "comment model" in rules function product_id is required, in this time if i post my form that time give an error

Please fix the following input errors:

Product cannot be blank.

if i product_id(foreign key for the comment table) remove from required then post the form this time give the another error i.e,

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (technoc1_notjustprice.tbl_comment, CONSTRAINT FK_comment_product FOREIGN KEY (product_id) REFERENCES tbl_product_index (product_id) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO tbl_comment (content, status, create_time, author, email, url) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5) .

how to remove these errors please help me…