Updating number from column based on %

I am trying to create a code that will subtract a specific percentage from users in a DB based on the amount they have. The more they have the greater the percentage will increase.

This is what I have but it isn’t working, so assume I am missing a little or a lot…

If possible I would also like it to display how many users were taxed in each echo statement.

$link = mysqli_connect("localhost", "*****", "*****", "*********"); 
  
if($link === false){ 
    die("ERROR: Could not connect to GBRP database.  "  
                . mysqli_connect_error()); 
} 
$sql = "SELECT * FROM users"; 

if(bank <= 1000000){
    echo "These folk are too poor to tax!"; 
} else if (bank <= 5000000 and bank < 1000000){
    $sql = "UPDATE users SET bank=bank*(100-5)/100";
	echo "5% Tax Sucessful";
} else if (bank <= 25000000 and bank > 5000000){
    $sql = "UPDATE users SET bank=bank*(100-10)/100"; 
	echo "10% Tax Sucessful";
} else if (bank <= 100000000 and bank > 25000000){
    $sql = "UPDATE users SET bank=bank*(100-15)/100"; 
	echo "15% Tax Sucessful";
} else if (bank <= 500000000 and bank > 100000000){
    $sql = "UPDATE users SET bank=bank*(100-20)/100"; 
	echo "20% Tax Sucessful";
} else if (bank <= 2000000000 and bank > 500000000){
    $sql = "UPDATE users SET bank=bank*(100-25)/100"; 
	echo "25% Tax Sucessful";	
} else {	
	
    echo "ERROR: Gal is a noob and was unable to execute $sql. "
                            . mysqli_error($link); 
}  
mysqli_close($link); 
?> ```

The main problem is you are not executing any of the sql queries, nor fetching data from the SELECT query, and the bank being used in the php code isn’t a php variable. You are missing several of the fundamentals you would need to know to do this. It also looks like you were confused on at least one > comparison.

Even if this is just a programming class assignment, what you are doing with the database needs to be done in a realistic way. Any ‘transaction’ that affects a user’s account needs to have a separate row inserted, with either a plus or minus amount. The account data would be stored in an ‘account’ or similarly named table, related back to the users through a user_id column.

Thank you for the response. That has provided no help at all. Bank is a table column and the code works fine when I execute on just one query without any Else ifs

I’ve cleaned it up a little which should work, will try later. If anyone can offer constructive help on the code that would be appreciated.

<?php
$link = mysqli_connect("localhost", "*****", "*****", "*********"); 
  
if($link === false){ 
    die("ERROR: Could not connect to GBRP database.  "  
                . mysqli_connect_error()); 
} 
$sql = mysql_query("SELECT bank FROM users"); 

if(bank <= 1000000){
    echo "These folk are too poor to tax!"; 
} else if (bank <= 5000000) && (bank > 1000000){
    $sql = "UPDATE users SET bank=bank*(100-5)/100";
	echo "5% Tax Sucessful";
} else if (bank <= 25000000) && (bank > 5000000){
    $sql = "UPDATE users SET bank=bank*(100-10)/100"; 
	echo "10% Tax Sucessful";
} else if (bank <= 100000000) && (bank > 25000000){
    $sql = "UPDATE users SET bank=bank*(100-15)/100"; 
	echo "15% Tax Sucessful";
} else if (bank <= 500000000) && (bank > 100000000){
    $sql = "UPDATE users SET bank=bank*(100-20)/100"; 
	echo "20% Tax Sucessful";
} else if (bank <= 2000000000) && (bank > 500000000){
    $sql = "UPDATE users SET bank=bank*(100-25)/100"; 
	echo "25% Tax Sucessful";	
} else {	
	
    echo "ERROR: Gal is a noob and was unable to execute $sql. "
                            . mysqli_error($link); 
}  
mysqli_close($link); 
?>```
Sponsor our Newsletter | Privacy Policy | Terms of Service