PHP准备语句不适用于intval()

时间:2016-11-19 08:05:45

标签: php int prepared-statement converter

请查看我的代码:

$insertStmt = $conn->prepare("INSERT INTO orders (OrderID, OrderTrackingNumber, OrderTotal, CustomerID) VALUES (?, ?, ?, ?)");
$insertStmt->bind_param("ssdi", $orderID, strval("Not Ship Yet"), $orderTotal, $userID);
if ($insertStmt->execute()) {
    $insertStmt = $conn->prepare("INSERT INTO ordersproducts (OrderID, ProductID, ProductSold) VALUES (?, ?, ?)");
    $updateStmt = $conn->prepare("UPDATE products SET ProductQuantity = ? WHERE ProductID = ?");
    foreach ($orderedProducts as $orderedProduct) {
        $productQuantity = intval($orderedProduct->ProductQuantity) - intval($orderedProduct->ProductAddedQuantity);
        $insertStmt->bind_param("sii", $orderID, intval($orderedProduct->ProductID), intval($orderedProduct->ProductAddedQuantity));
        $updateStmt->bind_param("ii", intval($productQuantity), settype($orderedProduct->ProductID, "integer"));
        if ($insertStmt->execute() && $updateStmt->execute()) {
            if ($updateStmt->affected_rows == 1) {
                $isSuccefull = TRUE;
            } else {
                $isSuccefull = FALSE;
                break;
            }

        } else {
            $isSuccefull = FALSE;
            echo $insertStmt->error . " | " . $updateStmt->error;
            break;
        }
    }
}

$updateStmt->bind_param行,如果我将$orderedProduct->ProductID转换为intval($orderedProduct->ProductID)的int,则updateStmt将无效($updateStmt->affected_rows = 0)。但是,我使用settype($orderedProduct->ProductID, "integer");然后它将像冠军一样工作。只有这个地方才能解决这个问题;其他人工作得很好。

为什么?

感谢您的帮助。

0 个答案:

没有答案
相关问题