首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试使用isset设置值,但仍然得到错误

尝试使用isset设置值,但仍然得到错误
EN

Stack Overflow用户
提问于 2018-10-12 11:01:06
回答 1查看 114关注 0票数 0

我对php非常陌生,并且尝试创建一个购物车页面。

Iv获得使用$_POST数组发送的产品Id、选项和价格。

我希望$ino保存Id的值--我是这样做的。

我有一个反复出现的错误。

注意:第84行上的未定义索引:3 in /home/sl0/s 3196040/public_html/wp/a3/cart.php,名称:注意:未定义的索引:3 in /home/sl0/s 3196040/public_html/wp/a3/cart.php,在第71行: Price:注意:未定义的指数:3 in /home/sl0/s 3196040/public_html/wp/A3/cart.php,行74**

我知道我应该"isset“$ino来检查它是否设置,但是我认为我已经设置了?

如果我手动输入类似于$pumps[3]['Title']的值,它就能工作,但是如果我使用类似于这个$pumps[$ino]['Title']的$ino变量,它就会失败。

这在$_POST数组中( Id => 3选项=> english qty => 3)

代码语言:javascript
复制
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数组的内容

代码语言:javascript
复制
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']含量

代码语言:javascript
复制
Array
(
    [0] => 
    [1] => 2 
    [2] => 9 
)

代码是混乱的,但它只是为了学习的目的,谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-12 11:06:47

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

而且,$ino = $_POST['submit'];行没有任何效果,因为您用foreach ( $_SESSION['cart'] as $ino )覆盖了这个变量。

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

代码语言:javascript
复制
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
?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52778133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档