if statement

ok I know exactly what I want to do and the best way to describe it is:

if $copper=1000 subtract 1000 from copper and add 1 to silver. I have no idea how to get it to work in an if statement though. Can anyone help me with this?

this also corresponds with if statements. Its echoing the elseif statement even though the users group is “0”

[php]<?php

$username = $_SESSION[‘user’][‘username’];
$sql = “SELECT group FROM users where username=$username”;
mysqli_query($conn, $sql);
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$_SESSION[‘group’]= $row[‘group’];

if($row[‘group’] = 0) {

echo “sorry”;

} elseif($row[‘group’] = 2) {

echo “admin”;
}

?>[/php]

A single equals is an assignment operator. A double equals is a comparison operator.

[php]<?php
if ($copper==1000){
$copper = 0;
$silver = $silver + 1;
}
?>[/php]

http://www.w3schools.com/php/php_operators.asp

But doesn’t it need to have a mysqli_query?

I told you what your problem is. Apply it to your code.

eh,

[php]<?php

$copper = 2400;
$silver = 6;

$silver += intval($copper) / 1000;
$copper = $copper % 1000;

echo “

Copper: $copper

”;
echo "

Silver: " . (int)$silver . “

”;[/php]

But, but, he said if copper ==1000, not 2400. He didn’t say anything about for each thousand.

ok [u][b][size=14pt]I know exactly what I want to do[/size][/b][/u] and the best way to describe it is:

if $copper=1000 subtract 1000 from copper and add 1 to silver.

so would this be correct?

[php] if($row[‘copper’] == 1000) {

$sql1="
UPDATE
	users
SET
	copper = 0;
	
	silver = silver + 1	
Where
	id = $username";
				

}[/php]

this is what I have, and it’s not working

[php] Copper:
<?php
$username= $_SESSION[‘user’][‘username’];
$sql= “SELECT copper FROM users where username=’$username’”;
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$_SESSION[‘copper’]= $row[‘copper’];
echo $row[‘copper’];

if($row['copper'] >= 1000) {

$copper1="
UPDATE
	users
SET
	copper = copper - 1000;
		
WHERE
	username = $username";
	
$copper2 = "
UPDATE
	users
SET
	silver = silver + 1
WHERE
	username = $username";
					
mysqli_query($conn, $copper1);
mysqli_query($conn, $copper2);	
}	
?>[/php]

One you can do an update on more than a single value. Not sure why you are making this a multiple query thing.

Two,
[php]WHERE username = $username";[/php]

$username is a string. So, that query has a syntax error. Use prepared statements.

You are pulling the values out of the database, but updating the database based on values stored in the database. When, if you are updating based on what is already there, would copper be greater than 1000 IF you check that when updating?

Sponsor our Newsletter | Privacy Policy | Terms of Service