PDO - Fatal Error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row:

PDO - Fatal Error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row:

我知道已经存在与我遇到的相同问题的问题,但这并不能帮助我解决我在 PDO 中遇到的问题。

所以,我想就如何修复代码以及如何修复它寻求帮助。

我想做的是使用我的 create.php 插入 userlv(field),方法是创建一个包含字段 (username),(password),(userlv) 的帐户到我的 Table [用户],但它不会让我得到这个错误。

(我知道我有来自 Table[accesslvl] 的 userlv 和来自 Table[users] 的 userlv 使 userlv-Table[accesslvl] 作为唯一键和 userlv-Table[用户]作为索引使他们建立关系)。

还在 phpmyadmin 中执行 INSERT INTO users (username, passw, userlv) VALUES ('username', 'passw', 'SUPERADMIN') 工作正常并添加值,但是在执行 INSERT 时INTO users (username, passw, userlv) VALUES (:username, :passw, :userlv) in create.php 出现错误。

此外,当从 create.php 中完全删除 userlv 并留下字段(用户名和密码)时,它将成功创建帐户。

所以是的,我能做什么,有什么解决办法吗?如果需要,我可以包含其他代码。提前致谢。


错误


Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 
1452 Cannot add or update a child row: a foreign key constraint fails (`ces`.`users`, CONSTRAINT `users_ibfk_1` FOREIGN KEY (`userlv`) REFERENCES `accesslvl` (`userlv`) ON UPDATE CASCADE) in C:\xampp\htdocs\includes\create-inc.php:87 
Stack trace: #0 C:\xampp\htdocs\includes\create-inc.php(87): PDOStatement->execute() #1 C:\xampp\htdocs\create.php(2): include('C:\xampp\htdocs...') #2 {main} thrown in C:\xampp\htdocs\includes\create-inc.php on line 87

代码创建-inc.php

<?php
// Include config.php file
require_once "includes/config.php";
 
// Define variables and initialize with empty values
$username = $passw = $repeat_passw = $userlv = "";
$username_err = $passw_err = $repeat_passw_err = $userlv_err = "";
 
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
 
    // Validate username
    if( empty(trim($_POST["username"])) ){
        $username_err = "Please enter a username.";
    } else{
        // Prepare a select statement
        $sql = "SELECT uid FROM users WHERE username = :username";
        
        if($stmt = $pdo->prepare($sql)){
            // Bind variables to the prepared statement as parameters
            $stmt->bindParam(":username", $param_username, PDO::PARAM_STR);
            
            // Set parameters
            $param_username = trim($_POST["username"]);
            
            // Attempt to execute the prepared statement
            if($stmt->execute()){
                if($stmt->rowCount() == 1){
                    $username_err = "This username is already taken.";
                } else{
                    $username = trim($_POST["username"]);
                }
            } else{
                echo "Oops! Something went wrong. Please try again later.";
            }

            // Close statement
            unset($stmt);
        }
    }
    
    // Validate password
    if(empty(trim($_POST["passw"]))){
        $passw_err = "Please enter a password.";     
    } elseif(strlen(trim($_POST["passw"])) < 12){
        $passw_err = "Password must have atleast 12 characters.";
    } else{
        $passw = trim($_POST["passw"]);
    }
    
    // Validate confirm password
    if(empty(trim($_POST["repeat_passw"]))){
        $repeat_passw_err = "Please confirm password.";     
    } else{
        $repeat_passw = trim($_POST["repeat_passw"]);
        if(empty($passw_err) && ($passw != $repeat_passw)){
            $repeat_passw_err = "Password did not match.";
        }
    }
    
        // Validate userlv
    if(empty(trim($_POST["userlv"]))){
        $userlv_err = "Please enter a userlv.";     
    } else{
        $userlv = trim($_POST["userlv"]);
    }
    
    // Check input errors before inserting in database
    if(empty($username_err) && empty($passw_err) && empty($repeat_passw_err) && empty($userlv_err)){
        
        // Prepare an insert statement
        $sql = "INSERT INTO users (username, passw, userlv) VALUES (:username, :passw, :userlv)";
        
         
        if($stmt = $pdo->prepare($sql)){
            // Bind variables to the prepared statement as parameters
            $stmt->bindParam(":username", $param_username, PDO::PARAM_STR);
            $stmt->bindParam(":passw", $param_passw, PDO::PARAM_STR);
            $stmt->bindParam(":userlv", $param_userlv, PDO::PARAM_STR);
            
            // Set parameters
            $param_username = $username;
            $param_passw = password_hash($passw, PASSWORD_DEFAULT); // Creates a passw hash
            $param_userlv = $userlv;
            
            // Attempt to execute the prepared statement
            if($stmt->execute()){
                // Redirect to login page
                header("location: login");
            } else{
                echo "Something went wrong. Please try again later.";
            }

            // Close statement
            unset($stmt);
        }
    }
    
    // Close connection
    unset($pdo);
}
?>

而不是

$stmt->bindParam(":username", $param_username, PDO::PARAM_STR);
$stmt->bindParam(":passw", $param_passw, PDO::PARAM_STR);
$stmt->bindParam(":userlv", $param_userlv, PDO::PARAM_STR);

创建一个关联数组并在其中插入值然后执行它并查看是否有效

$paramArray = array (
     'username' => $username,
     'passw' => password_hash($passw, PASSWORD_DEFAULT), // Creates a passw hash
     'userlv' => $userlv );
$stmt->execute($paramArray);
$stmt->closeCursor();