Cdbcommand Failed To Execute The Sql Statement:after Upload The Image

I have a MySQL database for tbl_image and tbl_product_index.When I am going to upload image in image model, everything is fine here but when I am going to upload image in image model it is showing error like this:

"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_image, CONSTRAINT tbl_image_ibfk_2 FOREIGN KEY (product_id) REFERENCES tbl_product_index (product_id) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO tbl_image (product_id) VALUES (:yp0)"

I don’t know why it is happening here.Here is my database tables

Table structure for table tbl_image


product_id varchar(30) NOT NULL,

image_index varchar(20) NOT NULL,

image_location varchar(50) NOT NULL,

PRIMARY KEY (image_index),

KEY product_id (product_id),

KEY image_location (image_location),

KEY image_index (image_index),

KEY image_index_2 (image_index)


– Constraints for dumped tables

– Constraints for table tbl_image

ALTER TABLE tbl_image

ADD CONSTRAINT tbl_image_ibfk_2 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 (


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)


– 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;

any help…below in files model,controller and view code for the image

please help is very important for me…

thanks in advance…

Plz read this carefully

and review your code.

For example


should be


and so on.

It’s saying that you’re trying to enter a record into tbl_image for which there is no corresponding record in tbl_product_index. Your foreign key means that the value for product_id must already exist in tbl_product_index before you can insert a record with that value into tbl_image.

Make sure that the tbl_product_index record is inserted first and that the product_id you’re attempting to use already exists in that table when you insert into tbl_image.

thanks keith,orey …

i am doing that job after that data is go to the database table(tbl_image)and not reach the images in this path "C:/xampp/htdocs/new/images.i think some problem to reach appropriate place.

i have found another error that is:


private ‘_name’ => string ‘ajit.jpg’ (length=8)

private ‘_tempName’ => string ‘C:\xampp\tmp\php4896.tmp’ (length=24)

private ‘_type’ => string ‘image/jpeg’ (length=10)

private ‘_size’ => int 11914

private ‘_error’ => int 0

private ‘_e’ (CComponent) => null

private ‘_m’ (CComponent) => null

( ! ) Fatal error: Call to a member function saveAs() on a non-object in C:\xampp\htdocs\notjustprice\protected\controllers\ImageController.php on line 106

Call Stack

Time Memory Function Location

1 0.0041 141168 {main}( ) …\index.php:0

2 0.0809 1101824 CApplication->run( ) …\index.php:13

3 0.0809 1101920 CWebApplication->processRequest( ) …\CApplication.php:162

i tried very much after that i am not able to reach my goal…

Any help is appreciated.

Thanks a lot for the support being given to keith and orey

again i give my files.