Getting each user in table and using them where module = - php

I have this table: tbl_module_bid
image:
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 also don't want them to be duplicated
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");
}

Related

How to validate user ID from $_SESSION and MySQL user ID

I have a problem with MySQL query and condition. The main problem is user with another ID can see / edit the same leads even though his id != owner.
I tried to change the vars and add '' or "", but none of these help.
$myuser_query = mysqli_query($conn,"SELECT * FROM users WHERE id = '".$_SESSION["id"]."'");
$myuser = mysqli_fetch_assoc($myuser_query);
$myleads = "SELECT * FROM leads WHERE owner = '".$myuser["id"]."' AND status = 1 OR status = 2 ORDER BY RAND() LIMIT 1";
$newleads = $conn->query($myleads);
if ($newleads->num_rows >= 1) {
(Here it's all the client side that's showing the date.)
You need to add parenthesis around the OR conditions in your query. Change this:
$myleads = "SELECT * FROM leads
WHERE owner = '".$myuser["id"]."' AND status = 1 OR status = 2
ORDER BY RAND() LIMIT 1";
To:
$myleads = "SELECT * FROM leads
WHERE owner = '".$myuser["id"]."' AND (status = 1 OR status = 2)
ORDER BY RAND() LIMIT 1";

How to Fix This SQL Loop (PHP)?

I have designed a loop but it's not working as I'd like it to, and I'm assuming there are better ways to do this, but I'm quite confused.
Basically, there are two tables.
BlogSubscribers and Users
The BlogSubscribers simply holds the User ID's for subscribers (correlating to the Users) table, and I am trying to fetch the "Gender" value of each user that is a subscriber, but that information is stored in the Users table.
The loop only goes through once (which is why I tried to loop it with integers) - how can I make this work + more efficient?
It'd be great if you could explain any answers you post so I know how the code works in future.
$ID is just a GET paramater through the url, i.e ?Blog=$ID
$GetSubInfo = mysqli_query($db, "SELECT * FROM BlogSubscribers WHERE BlogID = '$ID'");
$GetSubInfoCount = mysqli_num_rows($GetSubInfo);
$LoopCount = 0;
while($LoopCount < $GetSubInfoCount){
$GetSubs = mysqli_fetch_object($GetSubInfo);
$GetFemaleSubs = mysqli_num_rows(mysqli_query($db, "SELECT * FROM Users WHERE ID = '$GetSubs->UserID' AND Gender = 'Female'"));
$GetMaleSubs = mysqli_num_rows(mysqli_query($db, "SELECT * FROM Users WHERE ID = '$GetSubs->UserID' AND Gender = 'Male'"));
$GetOtherSubs = mysqli_num_rows(mysqli_query($db, "SELECT * FROM Users WHERE ID = '$GetSubs->UserID' AND Gender = 'Other'"));
$GetUnknownSubs = mysqli_num_rows(mysqli_query($db, "SELECT * FROM Users WHERE ID = '$GetSubs->UserID' AND Gender = 'Unknown'"));
$LoopCount = $LoopCount + 1;
}

getting all mysql results that is less than given id

i am working on a timeline for my website but i am having some problem when i ran the query to select all id that is less than given identifier its still return the identifier result upon every query
example if identifier is id=4 i want to select everything less than 4 and not from 4 > 3 > 2 > 1 i want it to be 3 > 2 > 1
here is my php. i know its not secure or what not but i have written it in prepared statement and get the same thign so i need some here.
if(isSet($_POST['lastmsg']))
{
$feed_id = mysqli_real_escape_string($con, $_POST['lastmsg']);
$get1 = mysqli_query($con, 'SELECT receiver FROM connection where sender="'.$_SESSION['userid'].'"');
$id_feed = array();
while($id_result1 = mysqli_fetch_array($get1)){
$id_feed[] = $id_result1['receiver'];
$ids1 = join(',', $id_feed);
$get_feed1 = mysqli_query ($con, "select * from feed where users in '".$ids1."' or users='".$_SESSION['userid']."' and 'feed_id' < '".$feed_id."' ORDER BY feed_id DESC LIMIT 2");
}
while($res1 = mysqli_fetch_array($get_feed1)){
echo $load = $res1['feed_id'];
}
}

MySQL Query in PHP error

I have a problem with an query that won't work.
The one that needs to set the rank to 2 works, but the one that needs to set vip to 1 doesn't work.
I just get an white page.
What is the problem?
<?php
session_start();
include ("includes/config.php");
$lid = $_SESSION['lid'];
$uQuery = mysql_query("SELECT * FROM users WHERE id = '".$lid."'");
while($uFetch = mysql_fetch_array($uQuery)){
$uuser = $uFetch['username'];
$umotto = $uFetch['motto'];
$ucredits = $uFetch['credits'];
$upixels = $uFetch['activity_points'];
$ubelcr = $uFetch['belcredits'];
$urank = $uFetch['rank'];
$ufigure = $uFetch['look'];
}
if($urank < '2'){
mysql_query("UPDATE users SET rank = 2 WHERE id = '".$lid."'");
mysql_query("UPDATE users SET vip = 1 WHERE id = '".$lid."'");
}
Header("vip.php?succes=1");
?>
mysql_query("UPDATE users SET rank = 2,vip = '1' WHERE id = '".$lid."'");
Not exactly what you're looking for, but looking at the queries you could simplify to:
session_start();
include ("includes/config.php");
if(isset($_SESSION['lid'])){
$lid = $_SESSION['lid'];
$query = "UPDATE users SET rank = 2, vip = '1' WHERE rank < 2 AND id = ".intval($lid);
$result = mysql_query($query) or die(mysql_error());
header('Location: vip.php?succes=1');
exit;
}

PHP loop using foreach

Here is the problem:-
I've got a table of results from sports matches called 'MatchResults' with the following fields
Player1
Player2
Player1GamesWon
Player2GamesWon
Player1Pts
Player2Pts
league_id
First I am running a query which will filter this table using a stored variable I already have, giving me a shorter collection of rows:-
$qry = "SELECT * FROM BadmintonMatchResults WHERE league_id = '$leagueid'";
I need to run through each row that is produced from the above query and use the values to update another table called 'LeagueTable'. The structure of LeagueTable is as follows:-
member_id
Played
Won
Lost
GamesWon
GamesLost
Difference
Points
These updates take the form of several SQL queries, for example:-
$qry = mysql_query("UPDATE LeagueTable SET Played = Played + 1 WHERE member_id = '$p1id'");
$qry = mysql_query("UPDATE LeagueTable SET Played = Played + 1 WHERE member_id = '$p2id'");
if($p1pts > $p2pts) {
$qry = mysql_query("UPDATE LeagueTable SET Won = Won + 1 WHERE member_id = '$p1id'");
$qry = mysql_query("UPDATE LeagueTable SET Lost = Lost + 1 WHERE member_id = '$p2id'");
}
else if($p2pts > $p1pts) {
$qry = mysql_query("UPDATE LeagueTable SET Won = Won + 1 WHERE member_id = '$p2id'");
$qry = mysql_query("UPDATE LeagueTable SET Lost = Lost + 1 WHERE member_id = '$p1id'");
}
$qry = mysql_query("UPDATE LeagueTable SET GamesWon = GamesWon + '$p1won' WHERE member_id = '$p1id'");
$qry = mysql_query("UPDATE LeagueTable SET GamesWon = GamesWon + '$p2won' WHERE member_id = '$p2id'");
$qry = mysql_query("UPDATE LeagueTable SET GamesLost = GamesLost + '$p1won' WHERE member_id = '$p2id'");
$qry = mysql_query("UPDATE LeagueTable SET GamesLost = GamesLost + '$p2won' WHERE member_id = '$p1id'");
$qry = mysql_query("UPDATE LeagueTable SET Difference = GamesWon - GamesLost");
$qry = mysql_query("UPDATE LeagueTable SET Points = Points + '$p1pts' WHERE member_id = '$p1id'");
$qry = mysql_query("UPDATE LeagueTable SET Points = Points + '$p2pts' WHERE member_id = '$p2id'");
Ignore any $variables I have as I'm currently doing this off data entered in a form, but want to alter it so that it uses rows from another database instead. I also know that my queries can be condensed and combined, my main problem is looping round this collection of queries multiple times for different sets of values.
My questions are:-
How to I reference the fields from the first table in the queries I need to run? Obviously just listing the field name 'Player1' isn't going to work.
How do I loop through each row and run the above 2 queries along with 8 more similar ones.
Note: Some of these queries are already nested inside an IF statement so any loop would have to go outside of that.
Any help would be much appreciated!
If you are going to do things that way then please tell me that you are sanitising the data before you use it in SQL statements. If I types " 0' or 1=1 " as my input and you used that the SQL would not do what you expect it to do.
To lop and then use the data by reference name from one tot he next you would simply do this:
if(mysql_num_rows($result)>0){
while($row = mysql_fetch_assoc($result)){
// ... your loop here code here
mysql_query("UPDATE LeagueTable SET Played = Played + 1 WHERE member_id = '" . $row['Player1'] . "'");
}
}
To be honest this still looks like you've got a very inefficient way of doing things as you are making your MySQL socket work very hard. I would consider how much calculations you need to store and how many can be worked out in an aggregate SQL query.

Categories