【问题标题】:Uncaught exception PDOException : integrity constraint violation error未捕获的异常 PDOException : 完整性约束违规错误
【发布时间】:2019-09-03 13:51:28
【问题描述】:

我有一个错误

致命错误:未捕获异常 'PDOException' 并带有消息 'SQLSTATE[23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(gymdatabase.nutrition,CONSTRAINT nutrition_ibfk_2 FOREIGN KEY (food_id) REFERENCES food (food_id) ON DELETE CASCADE ON UPDATE CASCADE)' 在 G:\5th term\Ecommerce\xampp\htdocs\app\models\Nutrition_model.php:11 堆栈跟踪:# 0 G:\5th 学期\Ecommerce\xampp\htdocs\app\models\Nutrition_model.php(11): PDOStatement->execute(Array) #1 G:\5th term\Ecommerce\xampp\htdocs\app\controllers\Nutrition .php(59): Nutrition_model->insert() #2 [内部函数]: Nutrition->create() #3 G:\5th term\Ecommerce\xampp\htdocs\app\core\App.php(38): call_user_func_array(Array, Array) #4 G:\5th 学期\Ecommerce\xampp\htdocs\index.php(5): App->__construct() #5 {main} 在 G:\5th term\Ecommerce\xampp\ 中抛出htdocs\app\models\Nutrition_model.php 第 11 行

所以我的数据库中有 2 个表。 在 food 表中有 food_id、name、description、path 列。 在营养表中有栏 nutri_id、user_id、food_id。 所以在营养表中有一个外键是food_id 而且我在两个表中都没有任何数据。 我多次遇到此错误,但主要是因为我的外键被限制删除,或者表中已经存在违反约束的数据,但事实并非如此。

public function insert()//for event
    {
        $stmt = $this->_connection->prepare("INSERT INTO nutrition(user_id, food_id) VALUES(:user_id, :food_id)");
        $stmt->execute(['user_id'=>$this->user_id,'food_id'=>$this->food_id]);
        return $stmt->rowCount();
    }

public function create()
    {

        if(isset($_POST['nutrition_action']))
        {       

            $nutrition = $this->model('Nutrition_model');
            $user_id = $_SESSION['user_id'];
            $nutrition->user_id = $user_id;

            $gender = $_POST['gender'];
            $type = $_POST['type'];     
            $cheating = $_POST['cheating'];


            if($cheating == "Hell Yes")//CH
            {
                $food = 1;

            }
            else if($gender == "Male")
            {
                if($type == "Lose Weight") //ML
                {
                    $food = 4;

                }
                else if($type == "Maintain Weight") //MM
                {
                    $food = 7;
                }
                else//MG
                {
                    $food = 10;
                }
            }
            else
            {
                if($type == "Lose Weight")
                {
                    $food = 0;
                }
                else if($type == "Maintain Weight")
                {
                    $food = 0;
                }
                else
                {
                    $food = 0;
                }
            }
            $nutrition->food_id = $food;
            $nutrition->insert();

            //header('location:/Nutrition/automatic_index');//send somewhere
        }
        else
        {
            $this->view('nutrition/nutrition_create');
        }


    }

【问题讨论】:

  • I don't have any data yet in both table... 这就是问题所在,对吧?在food 表中添加一些食物,这样nutrition 中的外键就可以使用了。
  • 我只是在食物表中放了一些数据但仍然得到同样的错误:(我的意思是与营养表中的数据相对应的数据
  • 你绝对是对的..!我在食物表中有一些数据,但这些数据与营养表中的数据不匹配!太感谢了!这是一个愚蠢的错误大声笑

标签: php sql pdo


【解决方案1】:

外键约束需要外表中的记录以进行 INSERT。

如果一条记录仍然从另一个表中引用并且具有 FK 约束,则不能删除该记录。

使用 ON DELETE CASCADE,如果您删除一条食物记录,所有具有此 food_id 的营养物质也将被删除。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多