0

I've been trying to get this to save the changes made to the database for a week now. Me, My teacher, or anyone else I've asked and figure out any reason why this wouldn't be working. I've validated and debugged this thing nine ways to Sunday. I get now errors anywhere but it won't change all i ever see is the old data... p.s everything else in the application works fine so I know it's not connection problems.

Input and validation statements

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");
    }

The Function it called

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();
}

Here's my form:

<?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 Answers0