代码发现错误,发送错误消息,但将表单数据提交到会话购物车

Code sees error, sends error message, but submits form data into session cart

星号行之间的代码检查空字段,并在表单字段上方显示错误消息。

用于检查购物车中是否已存在 ID 的类似代码就在其下方。两者之间的区别在于所讨论的代码未按预期运行。它解释一个错误,它看到提交了一个空字段,并触发它的错误消息。

问题是它还将空字符串提交到购物车中。我不知道为什么。

我包含了所有代码,如果有人想测试的话。我希望有人能看到我看不到的东西。谢谢。

<?php
session_start();

// If no cart exists, create $_SESSION['cart'] array 
if(!isset($_SESSION['cart'])){
    $_SESSION['cart'] = array();
}

// Add item to array
if(isset($_POST['action']) && $_POST['action'] === 'add'){

/***************************************************/  
    // Check if input is empty / null
    if(empty($_POST['id'])){
        $error = '*Please enter an ID number.';
    }
/***************************************************/

    // Check if form data exists
    if(isset($_POST['id'])){
        $id = $_POST['id'];
    }

    // Check if ID already in cart
    if(isset($_SESSION['cart'][$id])){
        $error = '*Item already in cart.';
    }

    // Add new ID to array (hard-code some data for test file)
    $newitem = array(     
        'id' =>  $id,
        'part_number' => '369A7170-11',
        'quantity' => '1'
    );

    // Add new data to cart with ID as key    
    $_SESSION['cart'][$id] = $newitem;  
}

 // Add item to array
if(isset($_POST['action']) && $_POST['action'] === 'update'){

    // Check if input is empty / null
    if(empty($_POST['id'])){
        $error = '*Please select item and quantity.';
        include 'error.html.php';
        exit();
    }

    // Check if form data exists
    if(isset($_POST['id']) && isset($_POST['quantity'])){
        $id = $_POST['id'];
        $quantity = (int)$_POST['quantity'];
    }

    $_SESSION['cart'][$id]['quantity'] = $quantity;              

}

// Remove item from array
if(isset($_POST['action']) && $_POST['action'] === 'remove'){

    // Check if form data exists
    if(isset($_POST['id'])){
        $id = $_POST['id'];
    }

    unset($_SESSION['cart'][$id]);
}

// Empty cart
if(isset($_POST['action']) && $_POST['action'] === 'empty'){
    unset($_SESSION['cart']);
}

// Initialize $count variable; get item count
$count = '';
if(isset($_SESSION['cart'])) $count = count($_SESSION['cart']);

// Display results
if(isset($_SESSION['cart'])){
    $show_cart = var_dump($_SESSION['cart']);
    echo $show_cart;
}  

?><!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Cart</title>
    </head>
<body>
    <h3>Cart Management</h3>
    <p style="color:#ff0000;"><?php if(isset($error)) echo htmlentities($error, ENT_QUOTES); ?></p>
    <p>Items in cart: <?php if(isset($count) && $count > 0)echo htmlentities($count, ENT_QUOTES); else echo 'none'; ?></p>
    <form action="" method="post">
        <label for="id">&nbsp;</label>
        <input type="text" name="id" id="id" placeholder="Enter ID number" autofocus>
        <input type="hidden" name="action" value="add">
        <input type="submit" value="Add">
    </form>

    <form action="" method="post">
        <label for="id">&nbsp;</label>
        <select name="id" id="id">
            <option value="">Select ID</option>
            <?php foreach($_SESSION['cart'] as $key => $item): ?>
            <option value="<?php echo htmlentities($item['id'], ENT_QUOTES); ?>"><?php echo htmlentities($item['id'], ENT_QUOTES); ?></option>            
            <?php endforeach; ?>
        </select>     
        <input type="hidden" name="action" value="remove">
        <input type="submit" value="Remove"> 
    </form>

    <form action="" method="post">
        <label for="id">&nbsp;</label>
        <select name="id" id="id">
            <option value="">Select ID</option>
            <?php foreach($_SESSION['cart'] as $key => $item): ?>
            <option value="<?php echo htmlentities($item['id'], ENT_QUOTES); ?>"><?php echo htmlentities($item['id'], ENT_QUOTES); ?></option>            
            <?php endforeach; ?>
        </select><br>
        <label for="quantity">&nbsp;</label>
        <input type="text" name="quantity" id="quantity" size="2">
        <input type="hidden" name="action" value="update">
        <input type="submit" value="Update quantity"> 
    </form>

    <form action="" method="post">
        <input type="hidden" name="action" value="empty">
        <input onclick="return confirm('Are you sure you want to empty the cart?');" type="submit" value="Empty cart"> 
    </form>
</body>
</html> 

所以你设置了 $error,然后打印它,没有别的。无论如何都会执行这一行(我认为这是将它添加到购物车的行):

$_SESSION['cart'][$id] = $newitem;

你必须在这样做之前检查错误。例如:

if(!isset($error))
    $_SESSION['cart'][$id] = $newitem;

在某些情况下,您要添加一个键为 NULL 的项目。这是修复:

// Check if form data exists
if(isset($_POST['id'])){
    $id = $_POST['id'];
} else {
    $id = null;
}

if ($id)
{
    // Check if ID already in cart
    if(isset($_SESSION['cart'][$id])){
        $error = '*Item already in cart.';
    } else {

       // Add new ID to array (hard-code some data for test file)
       $newitem = array(     
        'id' =>  $id,
        'part_number' => '369A7170-11',
        'quantity' => '1'
       );

       // Add new data to cart with ID as key    
       $_SESSION['cart'][$id] = $newitem;
    }
}  

我们的想法是,如果 id 为空,我们不想将任何条目添加到购物车。另外,为了安全起见,如果 $newitem 已经存在,请不要重新定义和添加它。