简体   繁体   中英

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

You're saying you need three variables in your PDO

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

But you're specifying four

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM