i die folgenden Fehler beim Versuch, die Join-Tabelle zu aktualisieren:ein Fremdschlüssel fehl Fehler
Notice: Undefined variable: id
Notice: Undefined variable: course_id
Cannot add or update a child row: a foreign key constraint fails (`school-project`.`students_courses`, CONSTRAINT `fk_courses` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE CASCADE)0 Row inserted.
die 3 Tabellen sind:
# Name Type Collation Attributes Null Default Comments Extra
1 id Primary Index int(11) No None AUTO_INCREMENT
2 student_id Index int(11) No None
3 course_id Index int(11) Yes NULL
# Name Type Collation Attributes Null Default Comments Extra
1 idPrimaryIndex int(11) No None AUTO_INCREMENT
2 name varchar(255) latin1_swedish_ci No None
3 phone varchar(20) latin1_swedish_ci No None
4 email text latin1_swedish_ci No None
5 image text latin1_swedish_ci No None
# Name Type Collation Attributes Null Default Comments Extra
1 idPrimaryIndex int(11) No None AUTO_INCREMENT
2 nameIndex varchar(20) latin1_swedish_ci No None
3 descr text latin1_swedish_ci No None
4 image text latin1_swedish_ci No None
der Code:
function joinTable() {
global $connection;
$join_table_sql = "INSERT INTO students_courses (student_id, course_id)
VALUES ('$id', '$course_id')";
if ($connection->query($join_table_sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $join_table_sql . "<br>" . $connection->error;
}
}
was fehlt mir? Warum bekomme ich diese Fehler ?? .
welche Werte sind in $ id und $ course_id? diese sollten student_id und course_id aus den entsprechenden Tabellen existieren –
Gut für den Anfang, gibt es keine Variablen mit dem Namen $ id und $ course_id in Ihrer Funktion zur Verfügung ... sieht aus wie Sie sollten weiterlesen variabler Bereich in PHP – CBroe
ah ja: das Setzen der error_reporting auf E_ALL und display_errors ON sollte dich warnen (in deiner Entwicklungsumgebung) für fehlende vars –