[SOLVED] What does this SQL error mean?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Bbob
Forum Commoner
Posts: 57
Joined: Sat Aug 07, 2010 4:46 am

[SOLVED] What does this SQL error mean?

Post by Bbob »

Cannot add or update a child row: a foreign key constraint fails (`hp`.`materialinventory`, CONSTRAINT `materialinventory_ibfk_1` FOREIGN KEY (`vendorid`) REFERENCES `vendorinfo` (`vendorid`) ON DELETE CASCADE ON UPDATE CASCADE)


Does that mean that the "vendorid" from materialinventory cannot have duplicate values?

I used phpmyadmin from WAMP to set the vendorid from materialinventory as an index, then connected it to the "vendorid" of vendorinfo.

Did I set it correctly? I need to have duplicate vendorid from material inventory so I can connect different items to its vendor.


Here's my SQL query
INSERT INTO materialinventory VALUES('', '$type','$description','','$unit','$unitcost','','$id', '$date'
Last edited by Bbob on Fri Aug 13, 2010 7:32 am, edited 1 time in total.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: What does this SQL error mean?

Post by JakeJ »

First of all, this makes no sense:
INSERT INTO materialinventory VALUES('', '$type','$description','','$unit','$unitcost','','$id', '$date'
How about: INSERT INTO materialinventory (field1, field2, etc.) VALUES ('$var1', '$var2', '$var3')

Secondly: If you're going to post code, please enclose it in the PHP Code tags. You can click the button for it in the formatting tool bar.

Thirdly, what your error means is that you're trying to change something that other things depend on and it would break it. SO you have to examine your query, find the dependencies, etc.

Look up some stuff on foreign keys, cascading and constraints.
Bbob
Forum Commoner
Posts: 57
Joined: Sat Aug 07, 2010 4:46 am

Re: What does this SQL error mean?

Post by Bbob »

Hi thanks for the reply
JakeJ wrote:First of all, this makes no sense:
INSERT INTO materialinventory VALUES('', '$type','$description','','$unit','$unitcost','','$id', '$date'
How about: INSERT INTO materialinventory (field1, field2, etc.) VALUES ('$var1', '$var2', '$var3')
I got that from w3schools and its working.
Secondly: If you're going to post code, please enclose it in the PHP Code tags. You can click the button for it in the formatting tool bar.
Sorry, I thought the PHP Code tag was only for PHP Code. Ill use it the next time I add any other code.
Thirdly, what your error means is that you're trying to change something that other things depend on and it would break it. SO you have to examine your query, find the dependencies, etc.
I manage to fix there error by using a session. The vendorid from the material inventory wasnt getting any value so I used a session to pass the value to the other page.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: [SOLVED] What does this SQL error mean?

Post by JakeJ »

This query works?
VALUES('', '$type','$description','','$unit','$unitcost','','$id', '$date'

A quote mark as a value and no closing parenthesis?

OHHHHH... just as I was typing that, i realized it's probably two single quotes and you just didn't paste in the ).

And sessions are great for passing data. Pretty much indispensable.

I'm glad you got your problem solved.
Post Reply