简体   繁体   中英

How to insert data into table with foreign key using html form and php?

Situation: user is logged in and wants to save their favorite color through html form. But in phpMyAdmin I can see that the foreign key and the primary key (which are columns 'user_id' in two separate tables) do not match. The foreign key shows NULL in the rows with data, while the primary key shows numbers (eg 3) in the rows with data.

As mentioned, there are 2 tables: (users & colors)

The following sql is used to create table colors:

CREATE TABLE colors ( 
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
favorite_color TEXT NOT NULL, 
user_id INT, 
FOREIGN KEY (user_id) REFERENCES users(user_id) 
); 

The following sql is used to create table users:

CREATE TABLE users (
    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(50) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

From the page where users insert data is welcome.php and it contains the following code:

<?php
session_start();
    
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
    header("location: login");
    exit;
}
?>

The html form:

<form action="welcome.php" method="post"> 
<label>My favorite color:
    <input type="text" name="favorite_color">
</label>
<input type="submit" value="Save">
</form>

And the php code to insert data:

<?php
$link = mysqli_connect("localhost", "root", "", "my_db");
 
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
 
$sql = "INSERT INTO colors (id, favorite_color, user_id) VALUES (?, ?, ?)";
 
if($stmt = mysqli_prepare($link, $sql)){
    mysqli_stmt_bind_param($stmt, "sss", $id, $favorite_color, $user_id);
    
    $id = $_REQUEST['id'];
    $favorite_color = $_REQUEST['favorite_color'];
    $user_id = $_REQUEST['user_id'];

    if(mysqli_stmt_execute($stmt)){
        echo "Records inserted successfully.";
    } else{
        echo "ERROR: Could not execute query: $sql. " . mysqli_error($link);
    }
} else{
    echo "ERROR: Could not prepare query: $sql. " . mysqli_error($link);
}
 
mysqli_stmt_close($stmt);
 
mysqli_close($link);
?>

What am I doing wrong? Any suggestion is welcome. Thanks.

I have been able to solve the problem.

Here's what I did:

I changed:

$sql = "INSERT INTO colors (id, favorite_color, user_id) VALUES (?, ?, ?)";
 
if($stmt = mysqli_prepare($link, $sql)){
    mysqli_stmt_bind_param($stmt, "sss", $id, $favorite_color, $user_id);

into:

$sql = "INSERT INTO colors (favorite_color, user_id) VALUES (?, ?)";
 
if($stmt = mysqli_prepare($link, $sql)){
    mysqli_stmt_bind_param($stmt, "si", $favorite_color, $user_id);

As you can see I removed 'id' and changed "sss" into "si".

And I changed:

$id = $_REQUEST['id'];
$favorite_color = $_REQUEST['favorite_color'];
$user_id = $_REQUEST['user_id'];

into:

$favorite_color = $_REQUEST['favorite_color'];
$user_id = $_SESSION['user_id'];

I removed 'id' entirely and I replaced REQUEST with SESSION for the column 'user_id'.

It is now showing matching numbers under 'user_id' in table colors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM