简体   繁体   中英

How to fix a 'foreign key constraint fails' in mysql

I'm setting a simple CRUD in PHP, i have created a two tables in phpmyadmin one for personal info (name, last name, email, phone), the other for additional phone numbers. I have a problem with setting the foreign key to connect the two tables. The additional phone number is dynamically added from the user with help of jQuery (it can be called upon or not depending if the user has or hasn't two phone numbers). Can someone point our where i am making the mistake. My knowledge of PHP is limited, and i create tables and databases from the GUI of phpmyadmin not with code.

The code that enters the added form input in the database.

// Checks if the submit button has been pressed, setst the variable values of the name values from the form
if (isset($_POST['submit'])){
  $first_name = $_POST['first_name'];
  $last_name = $_POST['last_name'];
  $email = $_POST['email'];
  $phone_number = $_POST['phone_number'];
$mysqli->query("INSERT INTO personal (first_name, last_name, email, phone_number) VALUES('$first_name', '$last_name', '$email', '$phone_number')")
      OR die($mysqli->error);
}
// Checks if the Add more numbers button has been pressed
 if (isset($_POST['phone_number2'])) {
   $phone_number2 = $_POST['phone_number2'];
// foreach statement to loop trought the array of inserted additional phone numbers
  foreach ($phone_number2 as $key => $value) {
      $mysqli->query("INSERT INTO phone (phone_number2) VALUES('$value')")
               OR die($mysqli->error);
}
}

Up until the point i enter the foreign key it works, and after i enter it i get the error. Cannot add or update a child row: a foreign key constraint fails ( exercise . phone , CONSTRAINT phone_ibfk_1 FOREIGN KEY ( user_id ) REFERENCES personal ( user_id ))

Use $mysqli->insert_id to get the ID of the user that was added in the previous query, and use that as the foreign key in the phone table.

if (isset($_POST['submit'])){
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $email = $_POST['email'];
    $phone_number = $_POST['phone_number'];
    $mysqli->query("INSERT INTO personal (first_name, last_name, email, phone_number) VALUES('$first_name', '$last_name', '$email', '$phone_number')")
        OR die($mysqli->error);
    $userid = $mysqli->insert_id;
// Checks if the Add more numbers button has been pressed
    if (isset($_POST['phone_number2'])) {
        $phone_number2 = $_POST['phone_number2'];
// foreach statement to loop trought the array of inserted additional phone numbers
        foreach ($phone_number2 as $key => $value) {
            $mysqli->query("INSERT INTO phone (userid, phone_number2) VALUES($userid, '$value')")
                OR die($mysqli->error);
        }
    }
}

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