Jan Nick
Jan Nick

Reputation: 11

I can't insert a foreign key into my database

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\Buchhandlung\gut.php on line 144 SQL Fehler in: INSERT INTO tblbücher (Titel, ISBN, Herausgabedatum, fVerlageID) VALUES(?, ?, ?, (Select VerlageID FROM tblverlage Where Name='Yen On'))

This is what my browser says as an error, but I don't know what I am doing wrong. I try to insert data into my table "tblbücher". The problem is, that I need to insert a foreign key but I cant display just "1, 2, 3, 4, 5,..." in the input form

    if ($titel!="" and $isbn!="" and $herausgabedatum!="" and $verlage!="" )
    {
    $sql=$dbh->prepare("INSERT INTO tblbücher (Titel, ISBN, Herausgabedatum, fVerlageID) VALUES(?, ?, ?, (Select VerlageID FROM  tblverlage Where Name='$verlage'))");
    $sql->execute(array($titel, $isbn, $herausgabedatum, $verlage)) or die("SQL Fehler in: ".$sql->queryString." <br /> ".$sql->errorInfo()[2]);
    echo"<h1>Folgende Daten wurden der Tabelle Orte hinzugefügt:</h1></br> ";
    echo "<table>";
    echo

"<tr><th>Titel</th><th>Herausgabedatum</th><th>Name des Verlags</th><th>fVerlageID</th></tr>";
echo "<tr><td>$titel</td><td>$isbn</td><td>$herausgabedatum</td><td>$verlage</td></tr>";

}

This is how my input form looks like. You see that I need to call the "Verlag" "Yen On" instead of the foreign key "1" because it looks much better, but it doesn't work

Upvotes: 0

Views: 73

Answers (1)

Orry
Orry

Reputation: 659

You're saying you need three variables in your PDO

VALUES(?, ?, ?, ...)

But you're specifying four

$sql->execute(array($titel, $isbn, $herausgabedatum, $verlage))

Upvotes: 1

Related Questions