【问题标题】:PHP script does not execute the MYSQL commandPHP脚本不执行MYSQL命令
【发布时间】:2021-08-23 03:35:40
【问题描述】:

我想用这个 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。由于某些原因无法正常工作(完全没有错误)

【问题讨论】:

    标签: php mysql pdo


    【解决方案1】:

    这个:

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

    应该是:

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

    不要将参数占位符放在任何类型的 SQL 引号内(即,不能使用单引号、双引号或反引号)。

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

    【讨论】:

    • 哦,是的,你是对的,我忘了那句话中的`,我是盲人。谢谢($code在代码前面已经定义好了)
    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    相关资源
    最近更新 更多