0

I'm trying to pull a decimal from my database, but when I try it's trying to make it an int and I don't know how to fix it because I'm stupid and I don't even deserve to be called a programmer. Can someone please help?

if (isset($_POST['Add'])) {

$Date = $_POST['Date'];
$Amount = $_POST['Amount'];
$LegalFees = 0.00;
$CheckID = $_POST['Check_ID'];

$DateAdded = date("Y/m/d", strtotime($_POST['Date']));

$result = mysqli_query($conn, "SELECT CheckID FROM checks WHERE CheckID='" . $CheckID . "'");
$CurrentPayment = mysqli_query($conn, "SELECT payments FROM checks WHERE CheckID='" . $CheckID . "'");

$TotalPayment = $Amount + $CurrentPayment;

if (mysqli_num_rows($result) > 0) {

    $sqlinsert = $conn->query("INSERT INTO payments (Date,Amount,LegalFees,CheckID)Values('{$DateAdded}','{$Amount}','{$LegalFees}','{$CheckID}')");
    $sqlupdate = $conn->query("UPDATE checks SET payments=" . $TotalPayment . " WHERE checkID=" . $CheckID . "");
} else {
    $_SESSION["CheckIDFail"] = "Yes";
}

}

So basically it's supposed to select the payment in the db which is a decimal and store it into $CurrentPayment, but it can't because it's trying to make the decimal an int.

The rest of the code I don't really have a problem with, it works as it should. Just need to know how to make it pull a decimal correctly. I think it has to do with fetch or something? I don't know, someone please help this pathetic person?

1 Answers1

1

I think the problem is trying to convert a result set (an array) to a number. PHP doesn't care whether your number is a decimal or an integer.

$Date = $_POST['Date'];
$Amount = $_POST['Amount'];
$LegalFees = 0.00;
$CheckID = $_POST['Check_ID'];
$DateAdded = date("Y/m/d", strtotime($_POST['Date']));

$result = mysqli_query($conn, 
                       "SELECT payments FROM checks WHERE CheckID='" . $CheckID . "'") 
          or die(mysqli_error($conn));  


$currentPayment = 0;

if (mysqli_num_rows($result) > 0){
    // Get the "row" from the "result set". 
    // While loop is only added to illustrate the method of iterating through the results
    // If you only expect one row in the output, this should still work fine
    while($row = mysqli_fetch_assoc($result)){
        $currentPayment += $row['payments'];
    }

    $TotalPayment = $Amount + $currentPayment;


    mysqli_query($conn, "INSERT INTO payments (Date,Amount,LegalFees,CheckID)
                     values('{$DateAdded}','{$Amount}','{$LegalFees}','{$CheckID}')")
                     or die(mysqli_error($conn));

    mysqli_query($conn, "UPDATE checks SET 
                     payments=" . $TotalPayment . " WHERE checkID=" . $CheckID . "") 
                     or die(mysqli_error($conn));
} 
else {
     // No rows returned
     $_SESSION["CheckIDFail"] = "Yes";
}


A couple of things:

  • Maintain consistency in the type of queries within a program (in fact, even within an application) as a whole. Try not to switch back and forth between procedural (mysqli_query()) and object oriented ($conn->query) approach.

  • Use Prepared Queries to prevent the threat of SQL injections in your query - How to create a secure mysql prepared statement in php?

  • It's all right to stumble once in a while :)

Community
  • 1
  • 1
Dhruv Saxena
  • 1,336
  • 2
  • 12
  • 29