简体   繁体   中英

Updating Users's Records in Database By Group

I have this table: tbl_module_bid

image:

http://i.stack.imgur.com/4hQBN.png

you see users: Ali2,Ali,blackbone,dickface,mhmd let's call for each one of them it's called $player and the sql query I wanna use in:

mysql_query("UPDATE `bbcsystem`.`tbl_admin` SET games_played = games_played + 1 WHERE username = $player");

I tried using this script below:

//Update Game Played (not working very good):
    $num_qry        =   "Select DISTINCT *  From tbl_module_bid where  user = '".$_SESSION['LOGIN_BALANCE_FRONT']['name']."' AND module = '$mod_id'  order by bid asc";
    $get_pick       =   $db->get_results($num_qry,ARRAY_A);
                foreach($get_pick as $arr_pic)
                {
                    $player = $arr_pic['user'];
                    mysql_query("UPDATE `bbcsystem`.`tbl_admin` SET games_played = games_played + 1 WHERE username = $player");
                }

Well, the what im trying to do is, Take all the Usernames in tbl_module_bid and for each user inside of the table, I shall update his info (games Played) in another table, im really new to stackoverflow and phpp.. Please Consider Helping my as a big favor to me :) Thanks.

Try something like this:

UPDATE tbl_admin a
INNER JOIN tbl_module_bid b on a.username = b.user
SET a.games_played = games_played + 1
WHERE b.user = $_SESSION['LOGIN_BALANCE_FRONT']['name']
      AND b.module = $mod_id

This does your both queries in one single query.

If @Filipe got it right then here is a clean version

<?php
$q = 'UPDATE tbl_admin AS a'
    .' INNER JOIN tbl_module_bid AS b ON a.username=b.user'
    .' SET a.games_played=games_played + 1 WHERE b.module = "'.$mod_id.'"';
$result = mysql_query($q);

don't forget to mark his answer as the answer

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