1

I want to update price on invoice with this PHP code (the config file is included, more SQL statements are executed above this):

             <?php

$finalprice = getInvoicePrice($code);
$codeErr = "";
$discount = "";
 
if ($_SERVER["REQUEST_METHOD"] == "POST") {
     
    if (empty($_POST["discount"])) {
       $codeErr = "Code can not be blank.";
    }else {
       $discount = test_input($_POST["discount"]);
    
       if ($discount == "FIVE" ) {
          $codeErr = "OK";
              $price = getInvoicePrice($code);
              $percentage = 100;
              $percentage = 100 - 5;
              $finalprice = $percentage / 100 * $price;
              $SQLChangePrice = $odb->prepare("UPDATE `invoices2` SET `price` = :price WHERE `code` = `:code`");
                $SQLChangePrice->execute(array(
                    ":price" => $finalprice,
                    ":code" => $code
                ));
       }else {
          $codeErr = "wrong code";
              $price = getInvoicePrice($code);
              $finalprice = $price;
       }
    }
 }
 
 function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
 }

        ?>

full code (html form):

<form method = "post" onsubmit="return submitDiscount();">
     <table>
        <tr>
           <td>code:</td>
           <td><input type = "text" name = "discount">
           <span class = "error"><?php echo $codeErr;?></span>
           </td>
        </tr>
            
        <td>
           <input type = "submit" name = "submit" value = "Submit"> 
        </td>
            
     </table>
        
  </form>

Whole script and things around are working, but the MYSQL exec. isnt working for some reasons (no errors at all)

1 Answer 1

1

This:

UPDATE `invoices2` SET `price` = :price WHERE `code` = `:code`

should be:

UPDATE `invoices2` SET `price` = :price WHERE `code` = :code

Don't put parameter placeholders inside any kind of SQL quote (that is, not single-quotes, double-quotes, or back-quotes).

I also notice you did not set any value for the PHP variable $code.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh yes, you are right, i forgot the ` in that quote, I am blind. Thank you ($code is defined earlier in the code)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.