即使它应该也无法更新数据库

时间:2016-10-31 06:19:03

标签: php mysql pdo

我一直试图让这个保存对数据库所做的更改一个星期了。我,我的老师,或者我曾经问过的任何其他人,并弄清楚为什么这不起作用。我已经通过9种方式验证并调试了这个东西到星期天。我现在得到任何错误,但它不会改变所有我见过的旧数据... p.s应用程序中的其他一切工作正常,所以我知道这不是连接问题。

输入和验证声明

else if ($action == 'save_changes') 
    $category_id = filter_input(INPUT_POST, 'category_id', FILTER_VALIDATE_INT);
        $code = filter_input(INPUT_POST, 'code');
        $name = filter_input(INPUT_POST, 'name');
        $price = filter_input(INPUT_POST, 'price');
        $product_id = filter_input(INPUT_POST, 'product_id', FILTER_VALIDATE_INT);
    if ($category_id == NULL || $category_id == FALSE || $code == NULL || 
        $name == NULL || $price == NULL || $price == FALSE) {
        $error = "Invalid product data. Check all fields and try again.";
        include('../errors/error.php');
    } else { 
        save_changes($product_id, $category_id, $code, $name, $price);
        header("Location: .?category_id=$category_id");
    }

它所称的功能

function save_changes($product_id, $category_id, $code, $name, $price) {
    global $db;
    $query = 'UPDATE products 
         SET categoryID = :category_id, productCode = :code, productName = :name, listPrice = :price
         WHERE productID = :id';
     $statement = $db->prepare($query);
     $statement->bindValue(':category_id', $category_id);
     $statement->bindValue(':code', $code);
     $statement->bindValue(':name', $name);
     $statement->bindValue(':price', $price);
     $statement->bindValue(':id', $product_id);
     $statement->execute();
//   $updateResult = $statement->rowCount();
     $statement->closeCursor();
}

这是我的表格:

<?php include '../view/header.php'; ?>
<main>
        <h1>Edit Product</h1>
        <?php  
            //echo "$productID, $productName, $productCode, $categoryID";
            //exit; 
        ?>
    <section>
        <form id="add_product_form" action="index.php" method="post">
            <input type="hidden" name="action" value="save_changes">

            <label>Category ID:</label>
            <input type="text" name="category_id" id="category_id" value="<?php echo $product['categoryID']; ?>">

            <br>

            <label>Code:</label>
            <input type="text" name="code" id="code" value="<?php echo $product['productCode']; ?>">

            <br>

            <label>Name:</label>
            <input type="text" name="name" id="name" value="<?php echo $product['productName']; ?>">

            <br>

            <label>List Price:</label>
            <input type="text" name="price" id="price" value="<?php echo $product['listPrice']; ?>">

            <br>

            <label>&nbsp;</label>
            <input type="submit" name="submit" value="Save Changes">
            <input type="hidden" name="id" id="id" value="<?php echo $product['productID']; ?>">

            <br>

            <p>
                <a href="index.php">View Product List</a>
            </p>
        </form>
    </section>
</main>
<?php include '../view/footer.php'; ?>

0 个答案:

没有答案