外键(新手)

I want to insert the foreign key of my customer_id in my feedback table. How should I do it?

customer table:

customer_id 
coach_id
customer_name

feedback table:

feedback_id
feedback1
feedback2
customer_id 

I want to do it where after user login, user insert information of the feedback it will automatically register the customer id.

This is my code after I login and want to register feedback:

<?php
session_name ('YourVisitID');
session_start();
$page_title = 'Feedback';
include('./header4.html');
//remember to delete.
echo "{$_SESSION['customer_name']}";
?>

<section id="main" class="wrapper">
<div class="container">
<form action = "feedback.php" method="post">
<div class="row uniform 50%">
        <div class="6u 12u$(xsmall)">
        <input type="text" name="weight" placeholder="Weight" 
        required autofocus/>
        </div>
        <div class="6u$ 12u$(xsmall)">
        <input type="text" name="height" placeholder="Height" 
        required autofocus/>
        </div>
        <div class="6u 12u$(xsmall)">
        <input type="text" name="water"  placeholder="Water Level%" 
        required autofocus/>
        </div>
        <div class="6u$ 12u$(xsmall)">
        <input type="text" name="body_fat" placeholder="Body Fat%" 
        required autofocus/>
        </div>
        <div class="6u 12u$(xsmall)">
        <input type="text" name="calorie" placeholder="Calorie" 
        required autofocus/>
        </div>
        <div class="6u$ 12u$(xsmall)">
        <input type="text" name="visceral" placeholder="Visceral Fat Level%" 
        required autofocus/>
        </div>
        <p><input type="submit" name="submit" value="Register" /></p>
        <input type="hidden" name="submitted" value="TRUE" />

</div>
</form>
</div>
</section>

<?php

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

require_once ('mysql_connect3.php');

function escape_data ($data){
    global $dbc;
    if (ini_get('magic_quotes_gpc')){
    $data = stripslashes($data);
    }
    return mysql_real_escape_string(trim($data), $dbc);
    }

    $error = array();
        $weight = escape_data($_POST['weight']);
        $height = escape_data($_POST['height']);
        $water = escape_data($_POST['water']);
        $calorie = escape_data($_POST['calorie']);
        $visceral = escape_data($_POST['visceral']);
        $fat = escape_data($_POST['body_fat']);

mysqli_close($con);
header("location: add_user.php?remarks=success");   
    if (empty ($errors)) {

    $query ="SELECT * FROM feedback WHERE weight ='$weight'";
    $result = mysql_query($query);
    if (mysql_num_rows($result) == 0) {

        $query = "INSERT INTO feedback (weight, height, body_fat, water, calorie, visceral, feedback_date) VALUES 
        ('$weight', '$height', '$water', '$calorie', '$visceral','$fat', NOW() )";
        $result = @mysql_query ($query);
        if ($result) {

        echo '<script>
            alert("Your feedback has been save");
            </script>';

        include ('./footer.html');
        exit();
        }else{
            echo '<script>
            alert("<h1 id="mainhead">System Error</h1>
            <p class="error">You could not give feedback due to a system error.
            We apologize for any inconvenience.</p>");
            </script>';
            echo '<p>'. mysql_error() . '<br /><br />Query: ' . $query . '</p>';
            include ('./footer.html');
            exit();
            }
            }
                }else{
                    echo '<script>
                            alert("<h1 id="mainhead">Error!</h1>
                            <p class="error">Please try again.</p>");
                            </script>';
                        }

                        mysql_close();
                        }

?>

<?php
include ('./footer.html');
?>

I think you should define in sql table schema.

After you define foreign key customer_id in feedback table

You can use PHP to select data from that table

When a customer logs in, just store its customer_id in the session, just as you store its customer_name at the moment. When the customer submits the feedback form, you simply provide the customer_id from the session.

    $query = "INSERT INTO feedback (weight, height, body_fat, water, calorie, visceral, feedback_date, customer_id) VALUES 
    ('$weight', '$height', '$water', '$calorie', '$visceral','$fat', NOW(), ".$_SESSION ['customer_id']. ")";
    $result = @mysql_query ($query);  

Couple of notes:

  1. Do not use mysql_*() functions any longer, they have been deprecated years ago and has been removed as of php7. Use mysqli or pdo instead.
  2. Use prepared statements with parameter binding to prevent sql injection attacks.
  3. Do not mix various mysql apis.