简体   繁体   中英

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 | (id is primary key) 2. movie table | (movie_id is primary key, id is foreign key)

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.

This is my process.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.

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. I assume that $_SESSION['id'] is the User Id of the currently Logged In User.

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