[英]Opencart 2.x Undefined Index product.php
I am trying to figure out an issue with an undefined index error when a product is saved or edited. 我试图找出保存或编辑产品时出现未定义索引错误的问题。
I have created a new entry in the product table in the database called disable_cart which is set to int(1) and has a default value of 0. 我在数据库中的产品表中创建了一个名为disable_cart的新条目,该条目设置为int(1),默认值为0。
On the product_form.tpl it is simply a checkbox. 在product_form.tpl上,它只是一个复选框。
<div class="col-sm-10">
<div class="checkbox">
<label>
<?php if ($disable_cart) { ?>
<input type="checkbox" name="disable_cart" value="1" checked="checked" id="input-disable_cart" />
<?php } else { ?>
<input type="checkbox" name="disable_cart" value="1" id="input-disable_cart" />
<?php } ?>
</label>
</div>
</div>
The code in the product controller 产品控制器中的代码
if (isset($this->request->post['disable_cart'])) {
$data['disable_cart'] = $this->request->post['disable_cart'];
} elseif (!empty($product_info['disable_cart'])) {
$data['disable_cart'] = $product_info['disable_cart'];
} else {
$data['disable_cart'] = 0;
}
And the model in the addProduct() function 还有addProduct()函数中的模型
public function addProduct($data) {
$this->event->trigger('pre.admin.product.add', $data);
. "', disable_cart = '" . (int)$data['disable_cart'] . "', date_added = NOW()");
Same in the editProduct() function. 与editProduct()函数相同。 It works fine on my localhost but only works on the dev server if the checkbox is ticked otherwise I get the following error
它在我的本地主机上工作正常,但仅在复选框选中时,才在开发服务器上工作,否则出现以下错误
Undefined index: disable_cart in /admin/model/catalog/product.php on line 134Warning: Cannot modify header information - headers already sent by (output started at /admin/index.php:80) in /system/library/response.php on line 12
Can't figure out the issue, any help is greatly appreciated. 无法解决问题,非常感谢您的帮助。
Thanks 谢谢
edit: The value does save in the DB but it still throws the error 编辑:该值确实保存在数据库中,但仍会引发错误
Your problem is due to the fact that an unchecked Checkbox will not be sent through post
您的问题是由于以下事实: 未选中的复选框将不会通过
post
发送
post['disable_cart']
. post['disable_cart']
。 When you post a new product or make an edit, this part of the code is called: 发布新产品或进行编辑时,代码的这一部分称为:
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { $this->model_catalog_product->addProduct($this->request->post);
$data['disable_cart']
has not been defined at all . $data['disable_cart']
根本没有定义 。 so the addProduct
function will throw an exception. 因此
addProduct
函数将引发异常。
The solution is to add this line of code: 解决方案是添加以下代码行:
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
// add this line to `add` and to `edit`
if(!isset($this->request->post['disable_cart'])) $this->request->post['disable_cart'] = 0;
$this->model_catalog_product->addProduct($this->request->post);
...
As for the warning, it is simply saying that you have a premature output due to the error being thrown. 至于警告,只是说由于抛出错误而导致输出过早。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.