简体   繁体   English

从一个表中检索用户 ID 并将其插入到另一个表中

[英]Retrieving user id from one table and inserting it into another table

Am working on a project where i got two tables 1. users table |我正在做一个项目,我有两个表 1. 用户表 | (id is primary key) 2. movie table | (id 为主键) 2. 电影表 | (movie_id is primary key, id is foreign key) (movie_id为主键,id为外键)

I want to get user_id from users table and want to insert movie related info when that user has logged in am using session_variables for this but i don't know if my code is correct or not i have tried following code but didn't get any result.我想从 users 表中获取 user_id 并希望在该用户登录时插入电影相关信息为此使用 session_variables 但我不知道我的代码是否正确我尝试过以下代码但没有得到任何结果。

This is my process.php这是我的过程。php

$errors = [];
$fav = "";
$rank = "";
$rewatched = "";
$status = "";
$recommend = "";

if (isset($_POST['submit'])) {
    if (empty($_POST['fav'])) {
        $errors['fav'] = 'Favourite required';
    }
    if (empty($_POST['rank'])) {
        $errors['rank'] = 'Rank required';
    }
    if (empty($_POST['rewatched'])) {
        $errors['rewatched'] = 'Rewatched required';
    }
    if (isset($_POST['status'])) {
        $errors['status'] = 'Status required';
    }
    if (isset($_POST['recommend'])) {
        $errors['recommend'] = 'Recommend required';
    }

    $fav = $_POST['fav'];
    $rank = $_POST['rank'];
    $rewatched = $_POST['rewatched'];
    $status = $_POST['status'];
    $recommend = $_POST['recommend'];

    // Select id from users and insert into movie
    if (count($errors) === 0) {
        $sql = "SELECT * FROM movie WHERE id = (SELECT id FROM users WHERE id = '".$_SESSION['id']."')";
        $query = "INSERT INTO movie SET fav=?, rank=?, rewatched=?, status=?, recommend=? WHERE id = '".$_SESSION['id']."'";
        $stmt = $conn->prepare($query);
        $stmt->bind_param('siiss', $fav, $rank, $rewatched, $status, $recommend);
        $result = $stmt->execute();

        if ($result) {
            $anime_id = $stmt->insert_id;
            $stmt->close();

            $_SESSION['id'] = $movie_id;
            $_SESSION['fav'] = $fav;
            $_SESSION['rank'] = $rank;
            $_SESSION['rewatched'] = $rewatched;
            $_SESSION['status'] = $status;
            $_SESSION['recommend'] = $recommend;
            $_SESSION['message'] = 'Details have been submitted successfully!';
            $_SESSION['type'] = 'alert-success';
        } else {
            $_SESSION['error_msg'] = "Database error: Could not update details";
        }
    }
}

In simple words i want to get id from users table and when that particular user has logged in and filled the form related to movie and submitted the results it should be stored in movie table with his/her user id.简而言之,我想从用户表中获取 id,当该特定用户登录并填写与电影相关的表单并提交结果时,它应该与他/她的用户 id 一起存储在电影表中。

Thank you谢谢

<?php

// Assuming $_SESSION['id'] is the User ID of the User Who Currently Logged in.

$errors = [];
$fav = "";
$rank = "";
$rewatched = "";
$status = "";
$recommend = "";

if (isset($_POST['submit'])) {
    if (empty($_POST['fav'])) {
        $errors['fav'] = 'Favourite required';
    }
    if (empty($_POST['rank'])) {
        $errors['rank'] = 'Rank required';
    }
    if (empty($_POST['rewatched'])) {
        $errors['rewatched'] = 'Rewatched required';
    }
    if (isset($_POST['status'])) {
        $errors['status'] = 'Status required';
    }
    if (isset($_POST['recommend'])) {
        $errors['recommend'] = 'Recommend required';
    }

    $fav = $_POST['fav'];
    $rank = $_POST['rank'];
    $rewatched = $_POST['rewatched'];
    $status = $_POST['status'];
    $recommend = $_POST['recommend'];
    $id = $_SESSION['id'];

    // Insert into Movie Table
    if (count($errors) === 0) {
        $query = "INSERT INTO movie SET fav=?, rank=?, rewatched=?, status=?, recommend=?, id =?";
        $stmt = $conn->prepare($query);
        $stmt->bind_param('siissi', $fav, $rank, $rewatched, $status, $recommend, $id);
        $result = $stmt->execute();

        if ($result) {
            // If Insertion Successful
        } else {
            // If Something Went Wrong
        }
    }
}

In the above code, when the user submits the form it will capture the form details and store it in movie table with Foreign key as User Id.在上面的代码中,当用户提交表单时,它将捕获表单详细信息并将其存储在电影表中,外键作为用户 ID。 I assume that $_SESSION['id'] is the User Id of the currently Logged In User.我假设$_SESSION['id']是当前登录用户的用户 ID。

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

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