Uncaught exception PDOException : integrity constraint violation error

211
April 13, 2019, at 10:20 PM

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

    }
Answer 1

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.

READ ALSO
How to create and link custom template for categories

How to create and link custom template for categories

I am trying to display child categories on category pages and then from child categories to their posts

172
In Laravel 5.8, how do you define a model factory in a package?

In Laravel 5.8, how do you define a model factory in a package?

I can't seem to find any good and up to date resources on how to accomplish this

121
Storing 2 API usernames/passwords in database & calling correct combo in db query

Storing 2 API usernames/passwords in database & calling correct combo in db query

I have an internal SOAP API function call to send out an email via SubscriberMailThis function is used inner office on the intranet

118