Update multiple variables in an IF Statement - php

I am having some trouble updating multiple variables inside an IF Statement and not sure where i'm going wrong.
Here is my code:
$id = $_POST['retrive_quantity'];
$n_quantity = $_POST['n_quantity'];
$result2 = mysql_query ("SELECT * FROM stock_control WHERE id = '$id' ");
while ($row1 = mysql_fetch_array($result2))
{
$quantity=$row1['quantity'];
$threshold=$row1['threshold'];
$emailSent=$row1['email_sent'];
$orders_status=$row1['orders_status'];
$orders=$row1['orders'];
}
if ($quantity + $n_quantity > $threshold && $emailSent == 2) {
$update=0 && $orders_status=0 && $orders=0;
} else {
$update=2 && $orders_status=1 && $orders=$orders;
}
mysql_query("UPDATE stock_control SET quantity=quantity + '$n_quantity',
email_sent = '$update', orders_status = '$orders_status', orders = '$orders'
WHERE id = '$id' ");
The first line of the IF Statement works and also with just one of the variables, but fails with multiple.

You can not update variables like this you have to separate with ;
if ($quantity + $n_quantity > $threshold && $emailSent == 2) {
$update=0;
$orders_status=0;
$orders=0; // or $update = $orders_status = $orders=0;
} else {
$update=2;
$orders_status=1;
//$orders=$orders; //no need for this
}

Declare the variable before used in if block
$id = $_POST['retrive_quantity'];
$n_quantity = $_POST['n_quantity'];
$update=2;
$orders_status=1;
$orders=0;
$result2 = mysql_query ("SELECT * FROM stock_control WHERE id = '$id' ");
while ($row1 = mysql_fetch_array($result2))
{
$quantity=$row1['quantity'];
$threshold=$row1['threshold'];
$emailSent=$row1['email_sent'];
$orders_status=$row1['orders_status'];
$orders=$row1['orders'];
}
if ($quantity + $n_quantity > $threshold && $emailSent == 2) {
$update=0;
$orders_status=0;
$orders=0;
} else {
$update=2;
$orders_status=1;
$orders=$orders;
}
mysql_query("UPDATE stock_control SET quantity=quantity + '$n_quantity',
email_sent = '$update', orders_status = '$orders_status', orders = '$orders'
WHERE id = '$id' ");

Related

Auto Repeat Isset Post Until Requirements met

I have a php code that updates mysql tables with results of a fight.
When you press the button fight you excetude the below:
if(isset($_POST['attack_creature'])){
if($monnewhealth > "0"){ //if Monster exists
if($charhealth > "0"){ //if Character is alive go to fight
$fightcreature = "UPDATE user_character SET current_action_points = current_action_points-$fight_action_points WHERE ID = $currentUser AND current_action_points>$fight_action_points";
$stmt = $con->prepare($fightcreature);
$stmt->execute();
if($totalinflicteddamagetocreature > "0") {
$monnewhealth = $monnewhealth - $totalinflicteddamagetocreature;
if($monnewhealth < "0") {
$monnewhealth = 0;
}
$updatenmonewhealth = "UPDATE user_character SET fight_creature_new_health = $monnewhealth WHERE ID = $currentUser";
$stmt = $con->prepare($updatenmonewhealth);
$stmt->execute();
}
if($monnewhealth <= "0"){
$lastFight = $now_time;
$updatecharlastfightkills = "UPDATE user_character SET character_last_fight = $now_time, character_kills = $charkills+1, character_gold = $chargold+$mongoldreward, character_current_xp = $charexp+$monxpreward, current_xp_reward = $monxpreward, current_gold_reward = $mongoldreward WHERE ID = $currentUser";
$stmt = $con->prepare($updatecharlastfightkills);
$stmt->execute();
$insertbattlelog1 = "INSERT INTO battle_log (ID, battle_log_date, battle_log_result, battle_log_enemy_name, battle_log_enemy_lvl, battle_log_gold, battle_log_xp, battle_log_event) VALUES ('$currentUser', '$now_time', '1', '$monname', '$monlvl', '$charlastgoldreward', '$charlastxpreward', 'You have Destroyed Level $monlvl $monname and earned $monxpreward XP and $mongoldreward')";
mysqli_query($con, $insertbattlelog1);
}
if($monnewhealth > "0"){ //if Monster still alive
if($totalinflicteddamagetocharacter > "0") {
$charhealth = $charhealth - $totalinflicteddamagetocharacter;
if($charhealth < "0") {
$charhealth = 0;
}
$updatecharnewhealth = "UPDATE user_character SET current_health = $charhealth WHERE ID = $currentUser";
$stmt = $con->prepare($updatecharnewhealth);
$stmt->execute();
}
if($charhealth <= "0"){
$updatecharlastfightdeaths = "UPDATE user_character SET character_last_fight = $now_time, character_deaths = $chardeaths+1 WHERE ID = $currentUser";
$stmt = $con->prepare($updatecharlastfightdeaths);
$stmt->execute();
$insertbattlelog2 = "INSERT INTO battle_log (ID, battle_log_date, battle_log_result, battle_log_enemy_name, battle_log_enemy_lvl, battle_log_event) VALUES ('$currentUser', '$now_time', '2', '$monname', '$monlvl', '$charlastgoldreward', '$charlastxpreward', 'You have been killed by Level $monlvl $monname')";
mysqli_query($con, $insertbattlelog2);
}
}
}
}
header('Location: hunt.php');
}
I don't know how to repeat this process until monhealth or charhealth reach 0 Zero.
Also I want to log how many rounds took to reach 0 Zero and log every round totaldamages.
Many thank you in advance,
Chris
You can write a function and call it recursively with a condition.
function doIt() {
if($condition > 0) {
doIt();
}
}
doIt();
instead of using if just use while so if the $monnewhealth is still greater than 0 i will rerun the code block. and also add new variable $rounds thats increase on each round. something like the code below.
$rounds = 0;
while ($monnewhealth > "0") { // if($monnewhealth > "0"){
$rounds++; // this will cound your rounds
while ($charhealth > "0") { // if($charhealth > "0"){
// your huge code block here
}
}
A while loop would work in this case:
$roundCount = 0;
while($monnewhealth > 0 && $charhealth > 0) {
++$roundCount;
// Your current code
}

Flow of loop not functioning properly

i have a code here that
if the $cost <= $row2['cost_disbursed'], it will execute the if statement here
but, it still deducts the values even if it is less than the value
while ( ($row = mysqli_fetch_array($query_result)) && ($row2 =
mysqli_fetch_array($query_result2)) ) {
if ( $cost <= $row2['cost_disbursed'] ) {
if ($cost <= $row['remaining']){
$sql= "UPDATE project set cost_disbursed = cost_disbursed - '$cost'
WHERE id = '$id'";
$result = mysqli_query($con,$sql);
$sql2= "UPDATE budget set remaining = remaining - '$cost' where id='1'";
$result2 = mysqli_query($con,$sql2);
echo "<script>window.alert('Balance successfully deducted!')
location.href = 'disbursed-Page.php'</script>";
}
}
else {
echo "<script>window.alert('Balance is not enough')
location.href = 'disbursed-Page.php'</script>";
}
}

PHP, using Joomla, algorithm, networking if else,

Hi, I have a question, when i ever insert this code
if ($pointsreach>=$tempunilevel ){
mysql_query("UPDATE `incentives_table` SET pointsreach = pointsreach +500 where userid = '$newsponsorid' ");
the if statement does not work anymore
if (count($items) >0 && $lev <= 10 ){ // found get sponsor id for the next computation
$sponsorid = $items->upline; //sponsor id
but when i remove the
if ($pointsreach>=$tempunilevel ){
mysql_query("UPDATE `incentives_table` SET pointsreach = pointsreach +500 where userid = '$newsponsorid' ");
the statement works perfectly ...
$query = " UPDATE `#__eds_incentives_table` SET `temp` = `temp` + 25 where `userid` = '$sponsorid' ";
$db->setQuery($query);
$db->query();
$mote = $this->entryuni($sponsorid, $lev + 1, +25);
}
else {
return 'ok';
}
This is the code
public function entryuni($newsponsorid = null, $lev = 2, $fpv = 0)
{
$db = & JFactory::getDBO();
$query = "SELECT upline,fslot FROM `table` where userid = '$newsponsorid' ";
$db->setQuery($query);
$items = $db->loadObject();
$items = (!empty($items)) ? $items : array();
$queryreach = mysql_query("SELECT * FROM incentives_table WHERE userid = '$newsponsorid' ");
$fetchreach = mysql_fetch_array($queryreach);
$pointsreach=$fetchreach['pointsreach'];
$tempunilevel=$fetchreach['temp_unilevel'];
if ($pointsreach>=$tempunilevel ){
mysql_query("UPDATE `incentives_table` SET pointsreach = pointsreach +500 where userid = '$newsponsorid' ");
if (count($items) >0 && $lev <= 10 ){ // found get sponsor id for the next computation
$sponsorid = $items->upline; //sponsor id
$query = " UPDATE `#__eds_incentives_table` SET `temp` = `temp` + 25 where `userid` = '$sponsorid' ";
$db->setQuery($query);
$db->query();
$mote = $this->entryuni($sponsorid, $lev + 1, +25);
}
else {
return 'ok';
}

PHP SQL User Profile Completeness calculation not working

I'm trying to show a profile completeness bar on the users account and the progress bar is showing but it's not adding the number values in order to calculate the percentage of completed fields ie:
if($row['title'] != '')
$completedTitle = 20;
My shortened code is as follows:
<?php
$result = mysql_query("SELECT title,name,surname,identityno,gender FROM cic_candidates WHERE id='$id' LIMIT 1");
while($row = mysql_fetch_assoc($result))
$maximumPoints = 100;
{
if($row['title'] != '')
$completedTitle = 20;
if($row['name'] != '')
$completedName = 20;
if($row['surname'] != '')
$completedSurname = 20;
if($row['identityno'] != '')
$dcompletedIdentityno = 20;
if($row['gender'] != '')
$completedGender = 20;
}
$percentage = ($dcompletedTitle+$completedName+$completedSurname+$completedIdentityno+$completedGender)*$maximumPoints/100;
echo "".$percentage."%";
?>
The percentage shows in the echo but the total is wrong - it's not taking the values of 20 points for each field that is completed and including them in the "addition" part of the percentage calculation. Please can you tell me where I'm going wrong - I've been trying to figure this out for 4 days and have googled this and read over 2000 forums but can't find the answer. Any help would be greatly appreciated.
I think you are getting it all wrong from your condition, try this... it worked for me
$sql = "SELECT title,name,surname,identityno,gender FROM cic_candidates WHERE id='{$id}' LIMIT 1";
$result = $DBconnection->query($sql);
$maxvalue = 100;
$point = 0;
if ($result) {
while($row = $result->fetch_assoc()){
if($row['title'] != ''){
$point1 = $point+20;
}elseif($row['title'] == ''){
$point1 = $point+=0;
}
if($row['name'] != ''){
$point2 = $point+20;
}elseif($row['name'] == ''){
$point2 = $point+=0;
}
if($row['surname'] != ''){
$point3 = $point+20;
}elseif($row['surname'] == ''){
$point3 = $point+0;
}
if($row['identityno'] != ''){
$point4 = $point+20;
}elseif($row['identityno'] == ''){
$point4 = $point+0;
}
if($row['gender'] != ''){
$point5 = $point+=20;
}elseif($row['gender'] == ''){
$point5 = $point+0;
}
// otherwise
}
}else{
echo "error completing query";
}
$pint = $point1+$point2+$point3+$point4+$point5;
$percentage = ($pint*100)/100;
echo $percentage."%";
use {} around single quotes in query
<?php
// fisrt select database
$result = mysql_query("SELECT title,name,surname,identityno,gender FROM cic_candidates WHERE id='{$id}' LIMIT 1");
//use of MYSQL_QUERY is deprecated so don't use it
//use mysqli_query instead of mysql_query
if (!$result) {
echo "Could not successfully run query " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while($row = mysql_fetch_assoc($result))
$maximumPoints = 100;
$point = 0;
{
if($row['title'] != '')
$point+=20;
if($row['name'] != '')
$point+=20;
if($row['surname'] != '')
$point+=20;
if($row['identityno'] != '')
$point+=20;
if($row['gender'] != '')
$point+=20;
}
$percentage = ($point*$maximumPoints)/100;
echo $percentage."%";
?>

INSERT INTO not populating database table

I have a script that checks the submitgame table and if both approve1 and approve 2 are not blank it inserts data into clanstats. There is no mysql_error it simply redirects to the header without inserting anything into the clanstats table, so I have no idea what is going on. Below is the code.
<?php
include("user.php");
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM submitgame WHERE id='$id'") or die(mysql_error());
$playerclan = $row['playerclan'];
$opponentclan = $row['opponentclan'];
$win = $row['win'];
$approve1 = $row['approve1'];
$approve2 = $row['approve2'];
if($win == "won") {
$win = 1;
$points = 2;
$win2 = 0;
$points2 = 1;
}
else {
$win = 0;
$points = 1;
$win2 = 1;
$points2 = 2;
}
if($approve1 != "" && $approve2 != "") {
$query=mysql_query("INSERT INTO clanstats (clan, points, wins) VALUES ('$playerclan', '$points', '$win')");
$query=mysql_query("INSERT INTO clanstats (clan, points, wins) VALUES ('$opponentclan', '$points2', '$win2')");
echo mysql_error($query);
}
else {
header("location:../approvegames.php");
}
mysql_close($con);
header("location:../approvegames.php");
?>
<?php
//first off are you connecting, ill presume so
include("user.php");
//sql injection!!!
$id = mysql_real_escape_string($_GET['id']);
$result = mysql_query("SELECT * FROM submitgame WHERE id='$id' limit 1") or die(mysql_error());
//you were missing this
$row=mysql_fetch_array($result);
$playerclan = $row['playerclan'];
$opponentclan = $row['opponentclan'];
$win = $row['win'];
$approve1 = $row['approve1'];
$approve2 = $row['approve2'];
if($win == "won") {
$win = 1;
$points = 2;
$win2 = 0;
$points2 = 1;
}else{
$win = 0;
$points = 1;
$win2 = 1;
$points2 = 2;
}
if($approve1 != "" && $approve2 != "") {
//you can have multiple inserts
$query=mysql_query("INSERT INTO clanstats (clan, points, wins) VALUES
('$playerclan', '$points', '$win'),
('$opponentclan', '$points2', '$win2')");
header("location:../approvegames.php");
//adding die after the header will make sure nothing else gets executed
die();
}else{
header("location:../approvegames.php");
die();
}
//no need to kill the connection as it will close when the script exits
?>
I think you are missing a line. Perhaps something like:
$row = mysql_fetch_row($result)

Categories