简体   繁体   English

如何使用 html 表单和 php 向带有外键的表中插入数据?

[英]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.情景:用户登录并希望通过 html 表单保存他们喜欢的颜色。 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.但是在 phpMyAdmin 中,我可以看到外键和主键(它们是两个单独表中的“user_id”列)不匹配。 The foreign key shows NULL in the rows with data, while the primary key shows numbers (eg 3) in the rows with data.外键在有数据的行中显示NULL,而主键在有数据的行中显示数字(例如3)。

As mentioned, there are 2 tables: (users & colors)如前所述,有 2 个表:(用户和颜色)

The following sql is used to create table colors:以下sql用于创建表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:下面sql用于创建表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:从用户插入数据的页面是 welcome.php,它包含以下代码:

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

The html form: html表格:

<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 代码:

<?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".如您所见,我删除了“id”并将“sss”更改为“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'.我完全删除了“id”,并将“user_id”列的 REQUEST 替换为 SESSION。

It is now showing matching numbers under 'user_id' in table colors.它现在在表 colors 中的“user_id”下显示匹配号码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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