php - 尝试使用 isset 设置值但仍然出现错误
问题描述
我对 php 很陌生,并试图创建一个购物车页面。
Iv 使用 $_POST 数组发送了产品 ID、选项和价格。
我希望$ino
保持 Id- 的值,我就是这样做的。
我收到一个反复出现的错误。
注意:未定义索引:第 84 行 /home/sl0/s3196040/public_html/wp/a3/cart.php 中的 3 名称:注意:未定义索引:/home/sl0/s3196040/public_html/wp/a3/cart 中的 3。第 71 行的 php 价格:注意:未定义的索引:第 74 行的 /home/sl0/s3196040/public_html/wp/a3/cart.php 中的 3**
我知道我想“isset” $ino 来检查它是否设置,但我认为我有?
如果我像这样手动输入值$pumps[3]['Title']
,但如果我像这样使用 $ino 变量则会失败$pumps[$ino]['Title']
。
这是在 $_POST 数组中( [Id] => 3 [option] => english [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
)
代码很乱,但仅用于学习目的,谢谢!
解决方案
您的代码在第 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
?>
推荐阅读
- jasper-reports - 表格中的 Jaspersoft 报表数据栏
- javascript - Javascript 显示不正确的日期
- python - lxml 忽略部分 HTML 标签
- java - Geeting 403 错误:PERMISSION_DENIED 尝试在 Java 中使用 People Api 创建联系人
- android - 自定义对话框未在布局外显示图标
- scala - 如何在不重复 Gatling 中的整个功能块的情况下重复查询参数更改的请求?
- ionic-framework - 如何获得正确的宽度
运行? - laravel - Laravel + Rabbit = Crontab vs Supervisor
- node.js - SSL 例程:ssl3_get_record:错误的版本号
- node.js - Nodejs:express-winston 日志记录问题