doutuohan6606 2019-04-12 18:35
浏览 96
已采纳

未捕获的异常PDOException:完整性约束违规错误

I am having an error of

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (gymdatabase.nutrition, CONSTRAINT nutrition_ibfk_2 FOREIGN KEY (food_id) REFERENCES food (food_id) ON DELETE CASCADE ON UPDATE CASCADE)' in G:\5th semester\Ecommerce\xampp\htdocs\app\models\Nutrition_model.php:11 Stack trace: #0 G:\5th semester\Ecommerce\xampp\htdocs\app\models\Nutrition_model.php(11): PDOStatement->execute(Array) #1 G:\5th semester\Ecommerce\xampp\htdocs\app\controllers\Nutrition.php(59): Nutrition_model->insert() #2 [internal function]: Nutrition->create() #3 G:\5th semester\Ecommerce\xampp\htdocs\app\core\App.php(38): call_user_func_array(Array, Array) #4 G:\5th semester\Ecommerce\xampp\htdocs\index.php(5): App->__construct() #5 {main} thrown in G:\5th semester\Ecommerce\xampp\htdocs\app\models\Nutrition_model.php on line 11

So there are 2 tables in my database. In the food table there are columns food_id, name, description, path. In the nutrition table there are columns nutri_id, user_id, food_id. So in the nutrition table there is a foreign key which is food_id and I don't have any data yet in both tables. I have this error many times but it was mostly because my foreign key was restrict to delete or there are already data in the table that violates constraints but this isn't the case.

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');
        }


    }
  • 写回答

1条回答 默认 最新

  • dqyat62284 2019-04-12 18:46
    关注

    A foreign key constraint requires a record in the foreign table for INSERT.

    A record cannot be deleted if it is still referenced from another table and has a FK constraint.

    With ON DELETE CASCADE, if you delete a food record, all nutrions with this food_id will be deleted as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 netty整合springboot之后自动重连失效
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击