简体   繁体   中英

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'. 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.

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. I have also tried combining the INSERT and SELECT queries and it still has no effect. 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'. 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?

Views is a very helpful feature.

For example, make a SELECT query to find needed rows:

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! :)

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