首页 > 解决方案 > 插入列表 ID 和项目

问题描述

我有点迷茫,找不到任何东西可以引导我朝着正确的方向前进,所以也许你们中的一些人有建议。我正在尝试在列表中添加项目。

首先,我有一个包含用户 ID 的用户表。这个 id 作为会话放置,所以当用户创建添加一个列表时,他们的 id 被插入到列表中,所以我知道该列表属于哪个用户。列表表也包含一个列表名称和一个从自动增量获得的唯一 ID。

列表:

id(from user)     ai        Listname
   1              1         List one
   1              2         List two
   3              3         List one

我的下一个表是在列表中添加项目。它包含来自列表的 ai,以及它自己的自动增量和项目上的名称。

ai(from list)   itemAi       itemName
  ?               ?            ? 

我没有得到的是插入它。假设我在我的表中创建了一个列表,我插入了一个项目名称,并且 itemAi 自动设置,我如何设置我当前所在的列表?我将其设置为会话还是正常的方法是什么?

更新:

$name = 'Test';
$sql = $con->prepare('INSERT INTO list (name) VALUES (?)');
$sql->bind_param("s",$name);
$sql->execute();
$sql->close();

$user = $_SESSION['user_id'];

$qq = $con->prepare('INSERT INTO user_list (user_id, list_id) VALUES (?,?)');
$qq->bind_param("ss", $user,????);
$qq->execute(); 
$qq->close();
$con->close();

标签: phpmysql

解决方案


我会尝试将您的表规范化为逻辑概念:用户、列表、项目等。然后,使用连接表在它们之间创建多对多关系。

表:

user: id (自动递增), 名字, 姓氏, ...

list: id (自动递增), name

item: id (自动递增), name

user_list: user_id ( FK touser.id ), list_id ( FK tolist.id )

user_list_item: user_id ( FK touser.id ), list_id ( FK tolist.id ), item_id ( FK toitem.id )

* FK = 外键。有一些方法可以缩短一点,但我会在这里使用显式结构来保持干净和分离。

然后,在 PHP 中,您可以跟踪您的用户以及他们当前在会话中处理的列表。

<?php

// After logging in
$_SESSION['user_id'] = ...;

// Choose which list the user is working on
$_SESSION['list_id'] = ...;

如果他们没有选择要使用的列表,请强制他们这样做。这是验证的一部分。

现在,每次用户想要进行更改时,都使用这些会话值。您的业​​务逻辑将是:

<?php
// Associate a list with a user
INSERT INTO `user_list` (user_id, list_id) VALUES (?, ?)
  ?: $_SESSION['user_id']
  ?: $_SESSION['list_id']

// Disassociate a list from a user
DELETE FROM `user_list` WHERE user_id = ? AND list_id = ?
  ?: $_SESSION['user_id']
  ?: $_SESSION['list_id']

// Insert a new item into a user's list
$itemId = INSERT INTO `item` (name) VALUES (?)
  ?: 'Bread'
INSERT INTO `user_list_item` (user_id, list_id, item_id) VALUES (?, ?, ?)
  ?: $_SESSION['user_id']
  ?: $_SESSION['list_id']
  ?: $itemId

// Clear our the items from a specific list
DELETE FROM `user_list_item` WHERE user_id = ? AND list_id = ?
  ?: $_SESSION['user_id']
  ?: $_SESSION['list_id']

// Empty all items for a given user, but keep the lists intact.
DELETE FROM `user_list_item` WHERE user_id = ?
  ?: $_SESSION['user_id']

// Remove a specific item from a user's list
DELETE FROM `user_list_item` WHERE user_id = ? AND list_id = ? AND item_id = ?
  ?: $_SESSION['user_id']
  ?: $_SESSION['list_id']
  ?: $itemId

假设用户插入一个新列表,我插入它的名称,列表上的一个 id 得到 Ai 然后我必须在 user_list 中插入会话 user_id 和 list_id,用户 id 是从会话设置的,但是怎么做我在新创建的列表中获得了 list_id?

好问题。大多数 RDBM 为您提供了获取最后插入的 ID 的功能。PDO 专门通过lastInsertId.

在这种情况下,您将发出 2 个插入语句。第一个将创建新列表,第二个将通过从会话中获取新创建列表的 ID 和当前用户 ID 将列表与用户相关联。

<?php

// Create a new list and assign it to a user

// The form was submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // Connection to your DB. 
    // See https://phpdelusions.net/pdo#dsn for a decent tutorial on PDO. 
    $pdo = new PDO(...);

    // Get the name from the submitted form
    $listName = $_POST['name'];

    // Create the list and get the ID of the newly created list.
    $stmt = $pdo->prepare('INSERT INTO `list` (name) VALUES (?)');
    $stmt->execute([$listName]);
    $listId = $pdo->lastInsertId();

    // Now, associate the new list with the user. 
    $stmt = $pdo->prepare('INSERT INTO `user_list` (user_id, list_id) VALUES (?, ?)');
    $stmt->execute([$_SESSION['user_id'], $listId]);
}

推荐阅读