0

I was trying to find out what was wrong with my code. This is the error I'm recieving

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

This is my code

<?php
$sql = "INSERT INTO stasjon (navn) VALUES ('skogen', 'voksenlia') "; 
$resultat = $kobling->query ($sql);

$sql ="SELECT * FROM stasjon WHERE navn = ('skogen')";
$resultat = $kobling->query ($sql);
while ($rad = $resultat->fetch_assoc()) {
  $stasjon_id = $rad['stasjon_id'];
}
$sql = "INSERT INTO linjestasjon (linje_nr, stasjon_id) VALUES ('1','$stasjon_id')";
$resultat = $kobling->query ($sql);
if($kobling->query($sql)) {
    echo "Spoerringen $sql ble gjennomfoert.";
} else {
    echo "Noe gikk galt med spoerringen $sql ($kobling->error).";
?>

Some of it is in Norwegian because That's the language of the database I'm making. I was trying to add values to two different tables (that had stasjon_id as a foreign key) Thanks in advance

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Please do not use tags that do not apply to your question. I removed the database tags as it is unclear which one you are actually using. Please add the tag of *only* the database you are actually using – John Conde Mar 24 '17 at 18:18
  • please do `describe stasjon` and `describe linjestasjon` then post results. this will help us identify the cause. – Dimi Mar 24 '17 at 18:19
  • 1
    1 column and 2 values; the math doesn't add up – Funk Forty Niner Mar 24 '17 at 18:21
  • Only 1 column defined in your first query but you are passing two values. – Simon K Mar 24 '17 at 18:22
  • 1
    and the brackets in this `WHERE navn = ('skogen')` are not required. Brackets are mostly used for subqueries which you do not have. – Funk Forty Niner Mar 24 '17 at 18:22
  • You should use the last insert id on `$resultat` that will give you its ID, without needing the next query. Depending on the driver, http://php.net/manual/en/pdo.lastinsertid.php, http://php.net/manual/en/mysqli.insert-id.php. – chris85 Mar 24 '17 at 18:28
  • Seems like we've either fallen onto deaf ears, or they left the question. – Funk Forty Niner Mar 24 '17 at 18:30
  • Possible duplicate of [Integrity constraint violation: 1452 Cannot add or update a child row:](http://stackoverflow.com/questions/14063652/integrity-constraint-violation-1452-cannot-add-or-update-a-child-row) – miken32 Mar 24 '17 at 19:51

1 Answers1

0

Foreign Key constraints checks in the value that you are inserting or updating to a particular filed exists in some other filed of another table. Suppose I have 2 tables as follows

CREATE TABLE TableA
(
    SeqNo INT PRIMARY KEY,
    Name VARCHAR(500
)

CREATE TABLE TableB
(
    SeqNo INT NULL FOREIGN KEY REFERENCES TableA(SeqNo),
    Name VARCHAR(50)
)

So when you insert a new record to the Tableb.SeqNo filed, The value should either be NULL or some value that exists in the TableA.SeqNo.

So Before inserting the values make sure that you are inserting the value that satisfy your foreign key constraint

Jayasurya Satheesh
  • 7,826
  • 3
  • 22
  • 39
  • Please look at their first query again and look at the comments under their question. Your answer might help, but it doesn't solve the syntax that was posted. The error comes from the first query. – Funk Forty Niner Mar 24 '17 at 18:27
  • Sorry, I missed that part. Try changing the first Query as follow $sql = "INSERT INTO stasjon (navn) VALUES ('skogen'),('voksenlia') "; – Jayasurya Satheesh Mar 24 '17 at 18:30