【问题标题】:PHP executes sql inside a false condition of IF statement with $_POSTPHP 使用 $_POST 在 IF 语句的错误条件内执行 sql
【发布时间】:2018-10-01 21:33:12
【问题描述】:

请帮助解决这个难以理解的错误:php 总是在 IF 中执行 SQL 更新,条件为 $_POST。

条件为假时:代码 i) 不执行 echo 命令,但 ii) 仍执行 sql 命令

if ($_POST["scanned_set"] != "saved") {    
    try {
        $conn = new PDO("mysql:host=$servername;dbname=abc", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully";    

        // Update

        $sql = "UPDATE `id_scan` SET `scan_count` = 10 WHERE `id_scan`.`id` = 1";

        // use exec() because no results are returned
        $conn->exec($sql);        
    } catch(PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
    }

    $conn = null; 
}

奇怪的是,如果我尝试使用“IF (1 ==2)”的 iF 条件,那么代码运行良好。也就是说,它不执行sql。

完整代码

<html>
<body> 

<?php

$servername = "localhost";
$username = "reviinve_vchain";
$password = "";

var_dump($_POST["scanned_set"]);

try {
    $conn = new PDO("mysql:host=$servername;dbname=reviinve_vchain", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 

    // Retrieve data from db
    $sql = "SELECT *  FROM `id_scan` WHERE `id` = 1";

        foreach ($conn->query($sql) as $row) {
        echo "print scan number after retrieving statement ".$row['scan_count'] . "\t";
        // print $row['color'] . "\t";

        $count_update = $row['scan_count'] + 1;     
        }        
}
    catch(PDOException $e){
        echo "Connection failed: " . $e->getMessage();
    }

    $conn = null;

if ($_POST["scanned_set"] != "saved") {
    try {
        $conn = new PDO("mysql:host=$servername;dbname=reviinve_vchain", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully"; 

        // Update count number to db

        echo 'new count number' . $count_update;     

        $sql = "UPDATE `id_scan` SET `scan_count` = $count_update WHERE `id_scan`.`id` = 1";

        // use exec() because no results are returned
        $conn->exec($sql);
    }
    catch(PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
    }
    $conn = null; 
}

?> 
</body>
</html>

【问题讨论】:

  • var_dump($_POST); 输出是什么?
  • 您的情况可能是真的,尝试发送一个正确的 POST 变量并使用 == 验证它(更容易);当然还有 Jon 的建议。
  • @JonStirling 我没有看到具有该名称的列
  • “当条件为假时:代码 i) 不执行 echo 命令,但 ii) 它仍然执行 sql 命令”
  • 听起来您在脚本的其他地方有相同的 SQL 正在执行,而您没有意识到这一点。

标签: php mysql pdo


【解决方案1】:

先尝试清理您的请求变量:

$do_update = !(trim(strtolower($_REQUEST["scanned_set"])) == "saved")

if ($do_update) {    
    try {
        $conn = new PDO("mysql:host=$servername;dbname=abc", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully";    

        // Update

        $sql = "UPDATE `id_scan` SET `scan_count` = 10 WHERE `id_scan`.`id` = 1";

        // use exec() because no results are returned
        $conn->exec($sql);        
    } catch(PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
    }

    $conn = null; 
}

【讨论】:

  • 我已经尝试过您的解决方案,但不幸的是它不起作用!
猜你喜欢
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-04
  • 2023-01-03
  • 1970-01-01
相关资源
最近更新 更多