Updating a temporary table - php

I'm trying to update a temporary table then query it in a later statement but I don't seem to be having any look. Below is the code I am using:
$updatepostgrad = "UPDATE t1
SET AssociatedModule = 'COMP102'
ORDER BY StudentRanking DESC
LIMIT $numberofstudents";
$perform9 = mysql_query($updatepostgrad);
To update the temporary table. Then to query it I use the code below:
$qry15 = "SELECT * FROM t1 WHERE AssociatedModule = 'COMP102'";
$result15 = mysql_query($qry15);
Obviously the table was created successfully early on in the code. When I use
var_dump($result15);
to try and gather information all I get is bool(false). Any ideas whats going wrong.
In case it helps the idea of querying the table is to perform the below:
while ($return4 = mysql_fetch_assoc($result15)) {
$id = $return4['ID'];
$qry15 = "UPDATE postgraduate SET AssociatedModule = 'COMP102' WHERE ID = '$id'";
$perform15 = mysql_query($qry15);

Related

SQL multiple table update issue

I'm trying to update two tables at the same time. The action completes successfully.However, it updates all records in the the table instead of the ones specified. I have tried the suggestions here, but to no avail. Any ideas what the issue might be? My code looks like below.
$sql = "UPDATE $receiverTable, $currUserTable
SET $currUserTable.originator = '$curr_username',
$receiverTable.originator = '$curr_username',
$currUserTable.status = '$currValue',
$receiverTable.status = '$currValue'
WHERE ($receiverTable.username = '$curr_username')
OR ($currUserTable.username = '$curr_username')";
You have no condition on your JOIN (you really shouldn't be writing JOINs with commas any more) and so every row gets JOINed to every other row, which means that they all have a JOIN to a row in which one of the WHERE conditions is true, hence they all get updated. Rewrite your JOIN with the appropriate condition and the problem will go away. Something like:
UPDATE $receiverTable
JOIN $currUserTable ON $currUserTable.somecolumn = $receiverTable.somecolumn
SET $currUserTable.originator = '$curr_username',
$receiverTable.originator = '$curr_username',
$currUserTable.status = '$currValue',
$receiverTable.status = '$currValue'
WHERE ($receiverTable.username = '$curr_username')
OR ($currUserTable.username = '$curr_username')
If there is no way that the tables can be JOINed, you will need to write the UPDATE as two separate queries.
Instead of doing this with a single update use a transaction to wrap two update commands, something like this.
BEGIN TRANSACTIONS;
UPDATE TABLE1
SET Col1 = 'SomeValue'
WHERE Cond1 = 'SomeCond';
UPDATE TABLE2
SET Col2 = 'SomeValue'
WHERE Cond2 = 'SomeCond';
COMMIT;
UPDATE
Following this I believe with phpi it would look like:
mysqli_autocommit($dbConnection, false);
$query1 = " UPDATE $receiverTable set originator = '$curr_username',
status = '$currValue' WHERE username = '$curr_username' "
$query2 = " UPDATE $currUserTable set originator = '$curr_username',
status = '$currValue' WHERE username = '$curr_username' "
mysqli_query($dbConnection, $query1);
mysqli_query($dbConnection, $query2);
mysqli_commit($dbConnection);

PHP Mysql query updating multiple table even if some table are empty

$update = ("UPDATE branch, custodian, atm_machine_list, computer_list
SET branch.branch_name = '$branch_name',
custodian.branch_name = '$branch_name',
atm_machine_list.branch_name = '$branch_name',
computer_list.branch_name = '$branch_name',
branch.branch_code = '$branch_code',
branch.branch_address = '$branch_address',
branch.manager = '$manager',
branch.contact = '$contact'
WHERE branch.branch_name LIKE '%$edit_id%'
AND custodian.branch_name LIKE '%$edit_id%'
AND atm_machine_list.branch_name LIKE '%$edit_id%'
AND computer_list.branch_name LIKE '%$edit_id%'");
I am trying to update multiple tables (branch, custodian, atm_machine_list, computer_list)
I want my query to still update even if some of the table are empty.
for example there is a branch_name from custodian and no branch name on atm_machine_list then it will still update disregarding atm_machine_list but updating custodian at the same time.
Hope someone will help me.
Thanks

PHP loop through array to update SQL database

I am trying to loop through an array ($lineup_selected) that corresponds to a player row in a database. For each player I would like to execute an UPDATE query to the database that adds the value of $submissions_selected to the total_picks column. I am struggling with the code as it fails to execute the query. Any help please?!
// Select team & formation
$team_selected = "team1";
$lineup_selected = array("player1", "player2", "player3");
$submissions_selected = 4000;
// Loop through and update total_picks for each player in database present in lineup_selected array
$player_picks_query = "SELECT full_name, total_picks FROM table WHERE team=$team_selected";
$result = mysqli_query($conn, $player_picks_query);
while($row = mysqli_fetch_assoc($result)) {
$player = mysql_real_escape_string($row["full_name"]);
$add_player_picks = "UPDATE table
SET total_picks = total_picks + $submissions_selected
WHERE full_name = '$player'";
}
why not:
UPDATE table
SET total_picks = total_picks + $submissions_selected
WHERE team = '$team_selected'
this way you have only one query to execute and let your database do the looping. Else you would first select some records and then have your database update each one of them to update the record.
I assume the fullname is unique. If not, it would mean your version can have the update-query modify multiple records each time and so my approach is invalid
-- and I seem to repeat a lot of the comments when stating to sanitize and escape your input to be save(r).
edit:
combined it should come to:
// set team & formation
$team_selected = "team1";
$lineup_selected = array("player1", "player2", "player3");
$submissions_selected = 4000;
$updatequery = "UPDATE table
SET total_picks = total_picks + ?
WHERE team= ?";
$stmt = mysqli_prepare($updatequery);
mysqli_stmt_bind_param($stmt, "is", $submissions_selected, $team_selected);
/* execute prepared statement */
mysqli_stmt_execute($stmt);
Myself I am more into the pdo approach, but syntax should be like this.
In your select request you have a team that is obviously a String. So, maybe you can try your request like : "SELECT full_name, total_picks FROM table WHERE team='$team_selected'"
I don't know if PHP is smart enough to put the quotes.
I think it will be better if you use only an update statement.
First of all you implode your array
$lineup_selected = array("player1", "player2", "player3");
$players='".implode("','",$lineup_selected )."';
Now you can update the table
$updateStmt="UPDATE table
SET total_picks = total_picks + $submissions_selected
WHERE full_name in (".$players.") and team=".$team_selected.";

Update two rows to add and subtract

I have a datatable with user information and a column called 'Bits'. There is a transfer function and I would like to transfer x amount of bits from the logged in user to the user they specify. I have the following function that works, but I would like to know if there is a better (simpler, cleaner, quicker?) way to do all of this, and possibly return the row of the first SQL statement at the end. I am not an advanced MySQL user by any means, but I am pretty sure there is a function I am missing that could make this much easier.
function transferBits($toid, $amount)
{
global $loggedInUser, $db;
$fromid = $loggedInUser->user_id;
$sql = "INSERT INTO `dl_Transfers` SET From_ID = '".$db->sql_escape($fromid)."',
`To_ID`='".$db->sql_escape($toid)."',
`Bits`='".$db->sql_escape($amount)."',
`When`=Now()";
$result = $db->sql_query($sql);
$sql2 = "UPDATE `dl_Users` SET Bits = Bits - '".$db->sql_escape($amount)."' WHERE `User_ID` = '".$db->sql_escape($fromid)."'";
$result2 = $db->sql_query($sql2);
$sql3 = "UPDATE `dl_Users` SET Bits = Bits + '".$db->sql_escape($amount)."' WHERE `User_ID` = '".$db->sql_escape($toid)."'";
$result3 = $db->sql_query($sql3);
}
I think you could hit the update in single query by joining on the previous record id from your insert on dl_Transfers:
UPDATE `dl_Transfers` r
JOIN `dl_Users` f ON ( r.From_ID = f.User_ID )
JOIN `dl_Users` t ON ( r.To_ID = t.User_ID )
SET f.Bits = f.Bits - r.Bits, t.Bits = t.Bits + r.Bits
WHERE r.Transfer_ID = ?;
Also as others stated in their comments, you should definitely use transactions here.
You might consider using a MySQL trigger to handle this. Basically, you would set it up so anytime there is an insert on dl_Transfers, it updates the appropriate rows in dl_Users.
http://dev.mysql.com/doc/refman/5.0/en/triggers.html

PHP/SQL SELECT statement using asterisk

I am having trouble getting an sql request to work. Without giving more details than needed,
$db_query = mysql_query(" select years,avg,best,win,top10,champs from `profile` where PLAYERID = '$monkey_id'");
works fine. However,
$db_query = mysql_query(" select * from `profile` where PLAYERID = '$monkey_id'");
doesn't return any results. The only change is that I'm trying to pull all fields instead of just those few. I'm at a loss to explain this. I taught myself all this stuff, so it's always possible I'm doing something dumb.
Edit:
Here's the rest of the surrounding code:
$db_query_inside = mysql_query(" select * from `profile` where PLAYERID = $monkey_id");
$db_query = mysql_fetch_array($db_query_inside);
$years_prev = $db_query['years'];
$avg_prev = $db_query['avg'];
$best_prev = $db_query['best'];
$win_prev = $db_query['win'];
$top10_prev = $db_query['top10'];
$champs_prev = $db_query['champs'];
Edit again:
Still don't know why it wouldn't work with *, but I just got what I needed done by listing the specific fields. It doesn't end up with any sort of error that can be gleaned from
die(mysql_error())
so I'm just giving up and working on stuff that reacts rationally.
Why not try:
$db_query = mysql_query(" select `profile` where PLAYERID = '$monkey_id'");
Let's do this, modify the following line to reflect below. See what the error says, if any. I tried it myself (your code) and it seems to work fine.
$db_query_inside = mysql_query(" select * from `profile` where PLAYERID = $monkey_id") or die(mysql_error());

Categories