在表中输入外键

I am working on a database where I am using an add to cart mechanism. Now I want a particular customer to log in, enter his details and then proceed to the shopping. I'm not sure what my query should be when I want to enter the customer details in the cart table.

My tables are:

CREATE TABLE customer ( 
    Cid int(11) NOT NULL AUTO_INCREMENT, 
    Name varchar(255) DEFAULT NULL, 
    Address varchar(255) DEFAULT NULL, 
    Phone varchar(11) DEFAULT NULL, 
    PRIMARY KEY (Cid) 
    ) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=latin1
CREATE TABLE cart ( 
    ID int(11) NOT NULL AUTO_INCREMENT, 
    cid int(11) DEFAULT NULL, 
    pid varchar(11) NOT NULL, 
    quantity int(11) NOT NULL, 
    PRIMARY KEY (ID), 
    KEY cid (cid), 
    CONSTRAINT cart_ibfk_1 FOREIGN KEY (cid) REFERENCES customer (Cid) 
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1                                   

The query I was previously running without the customer login details was

INSERT INTO cart (pid, quantity) VALUES ('".$row{'code'}."', '".filter_input((INPUT_POST), "quantity")."') 

here code is obtained from the products table Any help on how to Insert the foreign key constraint will be appreciated. Thanks.

After customer login you will get customer id, so update cart table

 INSERT INTO cart (pid,cid, quantity) VALUES ('".$row{'code'}."', '".<customerid>."', '".filter_input((INPUT_POST), "quantity")."') 

You can get the from session as after login you should store the logged userid in session.

Here is the steps: (please consider the sql injection & safety issue also) In file1.php

<?php
session_start();
$sql = "<your insert query> to insert data in customer table"
$_SESSION["cid"] = mysqli_insert_id();

In fil2.php

<?php
    $sql = " INSERT INTO cart (pid,cid, quantity) VALUES ('".$row{'code'}."', '".$_SESSION["cid"]."', '".filter_input((INPUT_POST), "quantity")."')"

I guess the working flow is like this:

  1. customer register with your site.
  2. On successful registration, you save his details to the customer table. so in the table you have values like cid, username, password etc. (i don't see any such fields as per your table schema. how do you make logins?)
  3. once customer logins to the site, you will validate the given details and check against your DB. if true you will store them to the session. say for example,

        $_SESSION['Cid'] = the_id_of_the_logged_in_user;
        $_SESSION['username'] = the_username_of_the_logged_in_user; 
    
  4. when a user adds something to cart, you fetch the customer id ($_SESSION['Cid'] ) from the session and store to the cart table

And, thats it.

when the customer adds something to cart you can fetch the customer id from the session

      $customerId = $_SESSION['Cid']; //assuming that you store  it to session as $_SESSION['customerId'] = 23; (where 23 is the id of the cutomer)

and save to the database

       INSERT INTO cart (pid,cid, quantity) VALUES ('".$row{'code'}."', '".$customerId."', '".filter_input((INPUT_POST), "quantity")."')