Hongsuk Choi
Hongsuk Choi

Reputation: 39

Uncaught exception PDOException : integrity constraint violation error

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


    }

Upvotes: 0

Views: 217

Answers (1)

Honk der Hase
Honk der Hase

Reputation: 2488

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.

Upvotes: 1

Related Questions