-2

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());

?>
  • 1
    The error means that you're trying to insert a value which doesn't match the foreign key. So... What value are you trying to insert? What are the valid values? – David Apr 03 '17 at 18:03
  • There are currently two events registered within the event table, these EventIDs are 1 and 2 which is exactly what the drop down list generates. However, when I submit this user form I get the error stated above. – Kelly McGovern Apr 03 '17 at 18:05
  • 1
    That doesn't answer the question of what value you're trying to insert into your table. You are currently operating under the assumption that whatever code you've written *must* be flawless and that there's no need to check it or debug it. That assumption is false. – David Apr 03 '17 at 18:06
  • The value I am trying to insert are those generated by the drop down, so in this case would be 1 or 2. – Kelly McGovern Apr 03 '17 at 18:09

1 Answers1

0

Your form elements need name attributes to send their data to the server. This one doesn't have one:

<select>

Give it the correct name attribute:

<select name="eventID">

Without it, the value isn't sent to the server and your query is trying to insert an empty value into the table.

Additionally, you have an HTML element that doesn't actually exist:

<id="eventID" name="eventID" />

You should probably just delete that one, it doesn't do anything.

Also, be aware that your code is wide open to SQL injection. You'll want to take a look at this question.

David
  • 208,112
  • 36
  • 198
  • 279