postgresdbs
postgresdbs

Reputation: 45

Update Query not updating field in table

I am currently struggling with the following :

$res = $db->uniquequery("SELECT distinct won_current,ctt,darkmatter FROM ".USERS."     WHERE `id` = '".$USER['id']."'");

$checkwon = $res['won_current'];
$ct=$res['ctt']; 
$dm=$res['darkmatter'];

These appear to be working. Now...

$dmgift=0;
while($checkwon>=1000)
{
    $ct=$ct+1;                       
    //incrementing $ct doesnt seem to work in  the loop but it works fine outside it                                         
    $checkwon=$checkwon-1000;

    //threshold ranges and setting of dmgift.
    switch($ct){
        case $ct=1 : $dmgift=25000;break;
        case $ct>=5 && $ct<10: $dmgift=50000;break;
        case $ct>=10 && $ct<15: $dmgift=75000;break;
        case $ct>=15 && $ct<20: $dmgift=100000;break;
        case $ct>=20 : $dmgift=150000;break;

    }
    $dm=$dm+$dmgift;

    //$db->query("UPDATE ".USERS." SET won_current=$checkwon,ctt=$checkthreshold,darkmatter=$dm WHERE `id` = ".$USER['id'].";");
$db->query("UPDATE ".USERS." SET won_current=$checkwon WHERE `id` = ".$USER['id'].";");
$db->query("UPDATE ".USERS." SET ctt='$ct' WHERE `id` = ".$USER['id'].";"); // this update query is not passing.db field remains the same

$db->query("UPDATE ".USERS." SET darkmatter=$dm WHERE `id` = ".$USER['id'].";");           


}

Kindly advise...the other 2 update queries are passing well...if you notice above the 3 updates I had a full query commented in...split it to see what is not working in update... I did echo and vardump...outside the loop they gave me the correct values...BUT inside the loop they have me 11111 instead of 5...for example...not sure what I'm doing wrong....I'm very new to php ( 2 days ? ) and would appreciate a solution...

Upvotes: 0

Views: 228

Answers (4)

JJJ
JJJ

Reputation: 33163

The problem is with the switch.

switch($ct){
    case $ct=1 : $dmgift=25000;break;
    ...
}

The first case changes $ct to 1, so its value is always 1 in the SQL query so it looks like the query doesn't work.

In any case switch doesn't work like that, you need separate if phrases:

if( $ct == 1 ) { 
    $dmgift=25000
}
else if( $ct>=5 && $ct<10 ) {
    $dmgift=50000;
}
else if( $ct>=10 && $ct<15 ) {
    $dmgift=75000;
}
else if( $ct>=15 && $ct<20 ) {
    $dmgift=100000;
}
else if( $ct>=20 ) {
    $dmgift=150000;
}

Also note that you don't have cases for $ct between 2 and 4.

Upvotes: 3

Anto
Anto

Reputation: 610

use single quotes for values of MySQL

$db->query("UPDATE ".USERS." SET won_current='$checkwon' WHERE id = '".$USER['id']."'");

Upvotes: -1

Saranya Sadhasivam
Saranya Sadhasivam

Reputation: 1294

Check for $ct value.

If $checkwon is not greater than or equal to 1000, $ct value will remain the same db value

$db->query("UPDATE ".USERS." SET ctt='" . $ct . "' WHERE `id` = ".$USER['id'].";"); 

Upvotes: 0

Devang Rathod
Devang Rathod

Reputation: 6736

Change your update query

$db->query("UPDATE ".USERS." SET won_current = '".$checkwon."' WHERE `id` = '".$USER['id']."'");
$db->query("UPDATE ".USERS." SET ctt = '".$ct."' WHERE `id` = '".$USER['id']."'");
$db->query("UPDATE ".USERS." SET darkmatter = '".$dm."' WHERE `id` = '".$USER['id']."'");

Remove ";" semicolon from here

Upvotes: -1

Related Questions