简体   繁体   English

如何使用'$ row'将特定值从一个数据库表插入另一个数据库表?

[英]How to insert a particular value from one database table into another using '$row'?

I am currently trying to make a system which selects a user at random from the table 'users' and appends it to another table 'agreeuser' or 'disagreeuser' depending on whether or not the user has the 'opinion' value of 'like' or 'dislike'. 我目前正在尝试建立一个系统,从“用户”表中随机选择一个用户,并将其附加到另一个表'agreeuser'或'disagreeuser',具体取决于用户是否具有'like'值'like'或者“不喜欢”。 I am doing this by using $row to select the full row where the user has the opinion of 'like', but it doesn't seem to be adding the data stored in '$row[username]' to the 'user' column of the 'agreeuser' or 'disagreeuser' table. 我这样做是通过使用$ row来选择用户有“喜欢”意见的完整行,但它似乎没有将'$ row [username]'中存储的数据添加到'user'列'agreeuser'或'disagreeuser'表。

I have already tried storing the '$row['username'] value as a variable and using this in the value aspect of the query, but it doesn't seem to have worked. 我已经尝试将'$ row ['username']值存储为变量并在查询的值方面使用它,但它似乎没有用。 I have also tried combining the INSERT and SELECT queries and it still has no effect. 我也尝试过组合INSERT和SELECT查询,它仍然没有效果。 Can anyone tell me what I am doing wrong, please? 有谁能告诉我我做错了什么,拜托? :) :)

if($_SESSION['pageLoaded'] != "true") {

    $selectLikesQuery = "SELECT * FROM users WHERE opinion = 'like' ORDER BY RAND() LIMIT 1";
    $likeSelectorResult = mysqli_query($userConnect, $selectLikesQuery);
    while($row = mysqli_fetch_assoc($likeSelectorResult)) {
        $removeCurrentAgreeContent = "TRUNCATE TABLE agreeUser";
        $addAgreeUserQuery = "INSERT INTO agreeUser (user) VALUE ('$row[username]')";
        mysqli_query($chatConnect, $removeCurrentAgreeContent);
        mysqli_query($chatConnect, $addAgreeUserQuery);
    }

    $selectDislikesQuery = "SELECT * FROM users WHERE opinion = 'dislike' ORDER BY RAND() LIMIT 1";
    $dislikeSelectorResult = mysqli_query($userConnect, $selectDislikesQuery);
    while($row = mysqli_fetch_assoc($dislikeSelectorResult)) {
        $removeCurrentDisagreeContent = "TRUNCATE TABLE disagreeUser";
        $addDisagreeUserQuery = "INSERT INTO disagreeUser (user) VALUE ('$row[username]')";
        mysqli_query($chatConnect, $removeCurrentDisagreeContent);
        mysqli_query($chatConnect, $addDisagreeUserQuery);
    }
    $_SESSION['pageLoaded'] = "true";
}

I need the username from 'users' to be inserted into the 'user' column of 'agreeuser'. 我需要将'users'中的用户名插入'agreeuser'的'user'列。 Thanks for any help, and apologies if I'm doing something stupid :) 感谢您的帮助,如果我做了一些愚蠢的事情,我会道歉:)

Why don't you use SQL views to just see needed data in "a virtual table", instead of creating duplicate data? 为什么不使用SQL视图只查看“虚拟表”中​​的所需数据,而不是创建重复数据?

Views is a very helpful feature. 视图是一个非常有用的功能。

For example, make a SELECT query to find needed rows: 例如,进行SELECT查询以查找所需的行:

SELECT * FROM users WHERE opinion = 'dislike'

If this select suits you, just add: 如果这个选择适合你,只需添加:

CREATE OR REPLACE VIEW v_agreeUsers AS SELECT * FROM users WHERE opinion = 'dislike'

And make the same for users who agree: 对于同意的用户也这样做:

CREATE OR REPLACE VIEW v_disagreeUsers AS SELECT * FROM users WHERE opinion = 'like'

To be honest, I don't understand why do you do random select and insert users only one by one. 说实话,我不明白你为什么一个接一个地随机选择和插入用户。

In case you want to get only one and random user, just run this query after you've already created views mentioned upper: 如果您只想获得一个随机用户,只需在创建了上面提到的视图后运行此查询:

SELECT * FROM v_agreeUsers ORDER BY RAND() LIMIT 1
SELECT * FROM v_disagreeUsers ORDER BY RAND() LIMIT 1

Good luck! 祝好运! :) :)

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

相关问题 如何使用 MySQL 中的关系将另一个数据库中的一个表插入另一个表? - How to insert one table to another from another database with relationships in MySQL? 如何将表的行从一个数据库插入到另一个数据库(使用两个PDO) - How to insert rows of a table from one database to another (with two PDO) 如何从一个日期到另一个日期计算长日并使用CI插入数据库表 - How to count long days from one date to another date and insert to table of database using CI 使用复选框从一个表中删除行数据并将删除的行数据插入另一个表中 - Delete row data from one table using check box and insert the deleted row data in another table 如何使用一种形式将另一个表中的 id 插入到一个表中? - How to insert id from another table to a table using one form? 将值从一个表插入到另一个表 - insert value from one table to another 将表中的许多行插入另一表中的唯一行 - Insert many rows from a table into one unique row in another table 将行从一个表移动到另一个表(插入值列表与列列表不匹配) - Moving a row from one table to another (Insert value list does not match column list) 如何使用 php、msqli 将行从一个表提交到另一个表 - How to submit row from one table to another using php, msqli 如何从另一个数据库 MYSQL 插入表 - How insert in table from another database MYSQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM