尝试使用isset设置值,但仍然出现错误

时间:2018-10-12 11:01:07

标签: php cart isset

我对php很陌生,并尝试创建购物车页面。

Iv使用$ _POST数组发送了产品ID,期权和价格。

我希望$ino保留Id的值,我也这样做。

我遇到一个经常性错误。

  

注意:未定义的索引:第84行的/home/sl0/s3196040/public_html/wp/a3/cart.php中的3   名称:注意:未定义的索引:第71行的/home/sl0/s3196040/public_html/wp/a3/cart.php中的3   价格:注意:未定义索引:第74行的/home/sl0/s3196040/public_html/wp/a3/cart.php中的3 **

我知道我应该“设置” $ ino以检查是否设置了它,但是我想我有?

如果我像$pumps[3]['Title']这样手动输入值,它会起作用,但是如果我像这样$pumps[$ino]['Title']使用$ ino变量,则失败。

这在$ _POST数组中([Id] => 3 [option] =>英文[qty] => 3

define("TITLE", "Product Page");

include_once('tools.php');
topModule('Business Name - Home', 'showSpecials()');

print_r($pumps[3]['Price']);
print_r($_POST);

// if i have the ID of the product, i need to turn it into a variables that the cart can display

if (isset ( $_POST ['Id'] )) {
    // Check the item is not already in the cart
    if (!in_array($_POST ["Id"], $_SESSION['cart'])) {
        // Add new item to cart
        $_SESSION ['cart'][] = $_POST["Id"];
    }
}

else if (isset ( $_POST ['delete'] )) { // a remove button has been clicked
    // Remove the item from the cart
    if (false !== $key = array_search($_POST['delete'], $_SESSION['cart'])) {
        unset($_SESSION['cart'][$key]);
    }

    // Empty Cart
    else if (isset ( $_POST ["delete"] )) { // remove item from cart
        unset ( $_SESSION ['cart'] );      
    }
}

if (isset ( $_SESSION ["cart"] )) {
    if (isset($_POST['submit'])) {
       $ino = $_POST['submit'];
    }
?>

<form action='(omitted link)'
target='_blank' method='post'
enctype=''>
<table>
    <tr>
        <th>Product</th>
        <th>Price</th>
        <th>Qty</th>
        <th>Action</th>
    </tr>
<?php
// Set a default total
$total = 0;
foreach ( $_SESSION['cart'] as $ino ) {
    ?>
<tr>
    <td>
        Name: <?php echo $pumps[$ino]['Title']; ?>  // error here
    </td>
    <td>
        Price: <?php echo $pumps[$ino]['Price']; ?> // error here
    </td>
    <td>
        Qty: <?php echo $_POST[$ino]['qty']; ?>  // error here
    </td>
    <td>
        <button type='submit' name='delete' value='<?php echo $ino; ?>'>Remove</button>
    </td>
</tr>
<?php
    $total += $pumps[$ino]['Price']; // error here
} // end foreach
?>

Total: $<?php echo $total; ?>
    <tr>
        <td colspan="2">Total: $<?php echo($total); ?></td>
        <td><input type='submit' value='Checkout' /></td>
    </tr>
    <tr>
        <td><button type='submit' name='clear'>Clear cart</button></td>
    </tr>
</table>
</form>
<?php  } ?>


<?php
  endModule(); // Now a function call
?>

我的$pumps数组的内容

Array
(


   [1] => Array
        (
            [OID] => 01PRO
            [Title] => ProphetX
            [Description] => The Sequential Prophet X is a potent fusion of samples-plus-synthesis and is Dave Smith�s most ground-breaking evolution of the Prophet yet.
            [Option] => 
            [Price] => $4,500 
            [Image] => 
        )
[2] => Array
    (
        [OID] => 03REV
        [Title] => ProphetRev 2
        [Description] => The Prophet Rev2 is Dave Smith�s reimagining of his Prophet �08 poly synth � a modern classic that has appeared on countless recordings and stages since its debut in 2007
        [Option] => 
        [Price] => $3,000 
        [Image] => 
    )

[3] => Array
    (
        [OID] => 03REV
        [Title] => Rev2 Desktop
        [Description] => The Prophet Rev2 desktop module is just as powerful and easy to use as its counterpart, the Prophet Rev2 keyboard.
        [Option] => 
        [Price] => $2,500 
        [Image] => 
    )

[4] => Array
    (
        [OID] => 04OB6
        [Title] => OB-6 
        [Description] => The OB-6� is a once-in-a-lifetime collaboration between the two most influential designers in poly synth history, Dave Smith and Tom Oberheim.
        [Option] => 
        [Price] => $2,000 
        [Image] => 
    )

[5] => Array
    (
        [OID] => 05OB6
        [Title] => OB-6 Desktop
        [Description] => The OB-6� desktop module is just as powerful and easy to use as its counterpart, the OB-6 Keyboard.
        [Option] => 
        [Price] => $1,500 
        [Image] => 
    )

[6] => Array
    (
        [OID] => 06PR6
        [Title] => Prophet 6
        [Description] => The Prophet-6 is Dave Smith�s tribute to the poly synth that started it all�the Sequential Prophet-5. But it�s not simply a reissue of a classic. Rather, as Dave puts it, �It�s the result of our effort to build the most awesome-sounding, modern analog poly synth possible.
        [Option] => 
        [Price] => $3,500 
        [Image] => 
    )

[7] => Array
    (
        [OID] => 07PR6
        [Title] => Prophet 6 Desktop
        [Description] => The Prophet-6 desktop module is every bit as powerful and easy to use as its counterpart, the Prophet-6 Keyboard. The module has all of the same controls as the keyboard version and provides the same immediacy and ease of use � with absolutely no menu diving. As with the Prophet-6 Keyboard, all parameters are at your fingertips, with full-sized knobs and switches and a comfortable, intuitive layout.
        [Option] => 
        [Price] => $2,000 
        [Image] => 
    )

[8] => Array
    (
        [OID] => 08PR2
        [Title] => Pro 2 
        [Description] => The Pro 2 is Dave Smith�s flagship mono synth, and as Dave himself puts it, his �most powerful mono synth ever.�
        [Option] => 
        [Price] => $4,000 
        [Image] => 
    )

[9] => Array
    (
        [OID] => 09TEM
        [Title] => Tempest 
        [Description] => Tempest is the brainchild of legendary instrument designers, Dave Smith and Roger Linn.
        [Option] => 
        [Price] => $3,000 
        [Image] => 
    )

)

$_SESSION['cart']

的内容
Array
(
    [0] => 
    [1] => 2 
    [2] => 9 
)

代码杂乱无章,但仅出于学习目的,谢谢!

1 个答案:

答案 0 :(得分:0)

您的代码在第71行上失败,该行是<?php echo $_POST[$ino]['qty']; ?>,因此您的$pumps变量很好,但是您的$ _POST变量不包含值为$ino的键。因此,您将必须检查此变量并相应地更改索引。

另外,$ino = $_POST['submit'];行无效,因为您用foreach ( $_SESSION['cart'] as $ino )覆盖了此变量

要确保存在,请执行以下操作:

foreach ( $_SESSION['cart'] as $ino ) {
    if (isset($pumps[$ino])) { 
    ?>
<tr>
    <td>
        Name: <?php echo $pumps[$ino]['Title']; ?>  // error here
    </td>
    <td>
        Price: <?php echo $pumps[$ino]['Price']; ?> // error here
    </td>
    <td>
        Qty: <?php echo $_POST[$ino]['qty']; ?>  // error here
    </td>
    <td>
        <button type='submit' name='delete' value='<?php echo $ino; ? 
     >'>Remove</button>
    </td>
</tr>
<?php
    $total += $pumps[$ino]['Price']; // error here
   }
} // end foreach
?>