什么可能导致这个PHP运行但不抛出错误?

时间:2017-03-13 22:14:48

标签: php sql

所以我正在使用PHP和SQL为学校开展这个项目,其中显示来自表格的数据我是从表单提交数据,目的是使用输入的内容更新数据行。形式,问题是程序一直在运行,并没有给我任何错误,但也没有更新数据。我想知道是否有人能解释为什么它不会抛出任何错误。这是我设置的查询的问题还是不仅仅是那个?

下面我将插入代码。我很欣赏任何可能出现问题的见解,谢谢

edit_product_form.php

<?php
   require('database.php'); 
   $product_id = filter_input(INPUT_POST, 'product_id', FILTER_VALIDATE_INT);
   $category_id = filter_input(INPUT_POST, 'category_id', FILTER_VALIDATE_INT);


   $query = 'SELECT *
             FROM products
             WHERE productID = :product_id';

   $statement = $db->prepare($query);
   $statement->bindValue(':product_id',$product_id);
   $statement->execute();
   $products = $statement->fetchAll();
   $statement->closeCursor();
?>




    <form action="edit_product.php" method="post"
          id="add_product_form">   <!--NOTE: this  ID is called this for CSS purpose--> 

        <!--NOTE: All These foreach statements display the selected product information in the value-->
        <label>Category:</label>
        <input type="text" name="category_id"
        <?php foreach ($products as $product) :?> 
            value="<?php echo $product['categoryID'];?>" 
        <?php endforeach;?>><br>

        <label>Code:</label>
        <input type="text" name="code" 
        <?php foreach ($products as $product) :?> 
            value="<?php echo $product['productCode'];?>" 
        <?php endforeach;?>><br>

        <label>Name:</label>
        <input type="text" name="name" 
        <?php foreach ($products as $product) :?>
            value="<?php echo $product['productName'];?>"
        <?php endforeach;?>><br>

        <label>List Price:</label>
        <input type="text" name="price" 
        <?php foreach ($products as $product) :?>   
            value="<?php echo $product['listPrice'];?>"
        <?php endforeach;?>><br>

        <label>&nbsp;</label>
        <input type="submit" value="Save Changes"><br>
    </form>

edit_product.php

<?php
// Get the product data
$product_id = filter_input(INPUT_POST, 'product_id', FILTER_VALIDATE_INT);
$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', FILTER_VALIDATE_FLOAT);

// Validate inputs
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('error.php');

} else {
require_once('database.php');

$query ='UPDATE products
         SET categoryID = :category_id, 
             productCode = :code, 
             productName = :name, 
             listPrice = :price
         WHERE productID = :product_id';

$statement = $db->prepare($query);
$statement->bindValue(':product_id', $product_id);
$statement->bindValue(':category_id', $category_id);
$statement->bindValue(':code', $code);
$statement->bindValue(':name', $name);
$statement->bindValue(':price', $price);
$statement->execute();
$statement->closeCursor();

// Display the Product List page
include('index.php');
}
?>

1 个答案:

答案 0 :(得分:-3)

您应该在执行查询后验证任何错误,您可以尝试使用sqlsrv_errors()。您可以在this link上查看更多相关信息。