-4

I wrote a simple program which calculates bonus for amount of work done. But at the end of the month where I need to make report I encountered strange behaviour of PHP calculating it. The goal is to give 0 bonus when work is under 30, give 30% between 30 and 40, and above 40 give 50%. But very strange thing appears when I execute the code.
For example: data1

Everything is good until 2018-12-20 and 2018-12-21 the result is duplicated. For the other users it appeared too.
For example: data2

The code looks like this:

require_once("database.inc.php");
$mysqli = new mysqli($servername, $username, $password, $dbname);
$mysqli->set_charset("utf8");

if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$packer = $_GET['pakowacz'];
$sql  = "SELECT aso, number, cast(date_of_scan as date) FROM $packer where DATE(date_of_scan) between '$_GET[date_from]' and '$_GET[date_to]' ";
$sql2 = "SELECT DATE(date_of_scan) AS Data, COUNT(*) AS amount FROM  $packer where DATE(date_of_scan) between '$_GET[date_from]' and '$_GET[date_to]' GROUP BY DATE(date_of_scan) ORDER BY Data ";

if ($result2 = mysqli_query($mysqli, $sql2)) {
    mysqli_free_result($result2);
} else {
    echo $mysqli->error.'<br />';
}

if ($result3 = mysqli_query($mysqli, $sql)) {
    $rowcount = mysqli_num_rows($result3);
    mysqli_free_result($result3);
} else {
    echo $mysqli->error.'<br />';
}

$data = date("Y/m/d");
echo 'Report of packer: '.$_GET['pakowacz']."<br />";  
echo 'Raport from: '.$_GET['date_from']."<br />";
echo 'Raport to: '.$_GET['date_to'].'<br />';
echo 'Amount of packed: '.$rowcount;
echo '<table class="sortable"><tr><td width="25">DATA</td><td width="110">amount</td><td width="110">bonus</td></tr>';
$result = $mysqli->query($sql2);

if ($result->num_rows > 0) {
    $bonuswhole = 0;
    while ($row = $result->fetch_assoc()) {
        $amount = $row["amount"];
        if ($amount < 30) {
            $bonus = '0';
        } else if(($amount > 30 && $amount <= 40)) {
            $bonus = $amount * 0.3;
        } else if($amount > 40) {
            $bonus = 12+(($amount-40)*0.5);
        }
        $bonuswhole += $bonus; 

        echo  '<tr>'.'<td width="110">'.$row["Data"].'</td>'.'<td width="110">'.$row["amount"].'</td>'.'<td width="110">'.$bonus.'</td>'.'</tr>';
    }
} else {
    echo $mysqli->error;
}

echo "</table>";
echo 'Calosc premii od '.$_GET['date_from'].' do '.$_GET['date_to']." :".$bonuswhole.'ST';
$result->free();
require_once('statystics.php');
$mysqli->close();

Did I make mistake? Do you need more info?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Please do not post images of code. [Images of code are not appropriate on StackOverflow](https://meta.stackoverflow.com/a/285557/3784008). You have access to the plaintext; please copy and paste the code into your question. – anothermh Dec 31 '18 at 22:50
  • Dear Kuba, We have coding standards defined for PHP. These standards are deisgned to make the code easy to read, clean and understandable to others. Maintaining clean code helps a lot to prevent typos, simple scoping mistakes or even syntax errors. Please familiarize yourself with the PSR-1 and [PSR-2](https://www.php-fig.org/psr/psr-2/) standards. Even better find an IDE which will help you constantly keep your code tidy. e.g. VS Code – Dharman Dec 31 '18 at 23:54
  • 3
    Also your code has multiple vulnerabilities. Two of them are SQL injections and XSS. [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1) & [What's the best method for sanitizing user input with PHP?](https://stackoverflow.com/questions/129677/whats-the-best-method-for-sanitizing-user-input-with-php) & [How to prevent XSS with HTML/PHP?](https://stackoverflow.com/questions/1996122/how-to-prevent-xss-with-html-php) – Dharman Jan 01 '19 at 00:00

1 Answers1

1

The problem in the following lines (when $amount>40)

}else if($amount>40){
$bonus=12+(($amount-40)*0.5);
}

You are not just giving 0.5 bonus BUT adding 12 and doing other things ...

instead you need to replace with

}else if($amount>40){
$bonus=$amount * 0.5;
}

Also your code didn't take in account when $amount = 30

Good luck

Ilyes Tounsi
  • 273
  • 2
  • 11
  • the thing is i don't want to the whole bonus was calculated that way. I need to to calculate bonus *0.3 to the 40 and above 40 the bonus increase but only for amount above 40, in data1 you can see that amount didnt even got to the 40 – Kuba Prokop Dec 31 '18 at 22:15
  • My bad i all was wrong thanks to lack of >=30. Thanks – Kuba Prokop Dec 31 '18 at 22:24
  • Please [reformat your code](https://stackoverflow.com/help/formatting) using the linked guide. – anothermh Dec 31 '18 at 22:45