I have created a user form which allows users to register participants. One of the fields on this user form is a drop down list which asks the user to select an event Id. Event Id is a foreign key and the values listed within the drop down list are generated from the Events table. This means that the user is unable to select anything other than an Event Id that has already been generated.
When I submit this user form I get the below error:
Failed. Cannot add or update a child row: a foreign key constraint fails
I know this error message typically means I am attempting to input a value into a foreign key field that does not exist, but the drop down list for eventID is generated from the event table, meaning it only shows up eventIds that have already been registered and do exist. Can anyone tell me what the problem is?
Any help would be greatly appreciated.
<!DOCTYPE html>
<?php
$link=mysqli_connect("localhost","root","");
mysqli_select_db($link,"event_planner");
?>
<html>
<head>
<title>Participant's Registration Page</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body
<div id="form">
<form action="partregisterprocess.php" method="POST">
<h1 align="center">Participant's Registration Page</h1>
<p>
Name:<br />
<input type="text" id="name" name="name" required />
</p>
<p>
Surname:<br />
<input type="text" id="surname" name="surname" required />
</p>
<p>
Date of Birth:<br />
<input type="date" id="dob" name="dob" required />
</p>
<p>
Age at Camp:<br />
<input type="text" id="age" name="age" required />
</p>
<p>
Gender:<br />
Male<input type="radio" value="male" name="gender" />
Female<input type="radio" value="female" name="gender" required />
</p>
<p>
Address:<br />
<input type="text" id="address" name="address" required />
</p>
<p>
Contact No:<br />
<input type="text" id="contact" name="contact" required />
</p>
<p>
Next of Kin:<br />
<input type="text" id="nextkin" name="nextkin" required />
</p>
<p>
Next of Kin's Contact No:<br />
<input type="text" id="nextContact" name="nextContact" required />
</p>
<p>
Attendance at Camp:<br />
<input type="text" id="attendcamp" name="attendcamp" required />
</p>
<p>
Attendance at Sunday School:<br />
<input type="text" id="attendschool" name="attendschool" required />
</p>
<p>
Comments:<br />
<input type="text" id="comments" name="comments" />
</p>
<p>
<input type="submit" id="btn" value="Register" />
</p>
<label> EventID </label>
<id="eventID" name="eventID" />
<select>
<?php
$res=mysqli_query($link,"SELECT * FROM events");
while($row=mysqli_fetch_array($res))
{
?>
E<option><?php echo $row["eventID"]; ?></option>
<?php
}
?>
</select>
</form>
</div>
</body>
</html>
<?php
$Name = $_POST['name'];
$Surname = $_POST['surname'];
$Date_of_Birth = $_POST['dob'];
$Age_at_Camp = $_POST['age'];
$Gender = $_POST['gender'];
$Address = $_POST['address'];
$Contact_No = $_POST['contact'];
$Next_of_Kin = $_POST['nextkin'];
$Kin_ContactNo = $_POST['nextContact'];
$Attendance_Camp = $_POST['attendcamp'];
$Attendance_School = $_POST['attendschool'];
$Comments = $_POST['comments'];
$eventID = $_POST['eventID'];
mysqli_connect("localhost", "root", "");
mysqli_select_db("event_planner");
$insert= mysqli_query("INSERT INTO participants VALUES ('','$Name','$Surname','$Date_of_Birth','$Age_at_Camp','$Gender','$Address','$Contact_No','$Next_of_Kin','$Kin_ContactNo','$Attendance_Camp','$Attendance_School','$Comments','$eventID')")
or die("Failed. ".mysqli_error());
?>