PHP 脚本不执行 MYSQL 命令

PHP script does not execute the MYSQL command

我想用这个 PHP 代码更新发票上的价格(包含配置文件,在此之上执行更多 SQL 语句):

             <?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;
 }

        ?>

完整代码(html 形式):

<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>

整个脚本和周围的东西都在工作,但是 MYSQL exec。由于某些原因无法正常工作(完全没有错误)

这个:

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

应该是:

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

不要将参数占位符放在任何类型的 SQL 引号中(也就是说,不要使用单引号、双引号或反引号)。

我还注意到您没有为 PHP 变量 $code 设置任何值。