我一直收到数据库错误错误:SQLSTATE [HY000]:常规错误:1366不正确的整数值

时间:2018-02-19 17:36:33

标签: php cakephp phpmyadmin

嘿我在我正在建立的网站上填写表格时不断收到以下错误,无法找出原因。

Database Error
Error: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: ' ' for column 'product_id' at row 1

SQL Query: INSERT INTO `ahbtest`.`user_products` (`product_id`, `user_id`) VALUES (' ', 8)

Notice: If you want to customize this error message, create app\View\Errors\pdo_error.ctp

以下是我的表单视图的代码:

<form method="post" action="/admin/users/add_favorites<?= isset($userId) ? "/" . $userId  : ""; ?>" enctype="multipart/form-data">
    <div class='row form-group'>
        <div class="col-md-12">
            <h3>Products</h3>
                <?php foreach($products as $product):
                    ?>
            <div class="col-md-3">
                <input id="checkBox"  name='data[Product][]' value=" <?php $product['Product']['id']; ?>" type="checkbox">
            <label><?php echo $product['Product']['name']; ?></label>
            </div>
                <?php
                endforeach; ?>
        </div>
    </div>



<?php // echo $this->Form->hidden('Product.id'); ?>
<?php // echo $this->Form->hidden('Product.parent_id', array('value' => $parentId)); ?>
<button type="submit" class="btn btn-primary"><i class="fa fa-check"></i> Save Changes</button>

</form>

这是我的表单控制器的代码:

public function admin_add_favorites($userId = null){

    ini_set('memory_limit', '2G');

        if ($this->request->is('post')) {

            $error = false;
            $this->loadModel('UserProduct');
            if(!isset($userId))
                $userId = $this->Auth->user('id');
            foreach($this->request->data['Product'] as $product)
            {
                $this->UserProduct->create();

                if(!$this->UserProduct->save(array('product_id' => $product, 'user_id' => $userId)))
                        $error = true;
            }
             if (!$error) {
                $this->Session->setFlash("Favorites have been successfully added.", 'flash_success');
                return $this->redirect('/admin/users/favorites');
            }
            else {
                   $this->Session->setFlash("One or more errors occurred. Please try again.", 'flash_error');
            }
        }
        if(isset($userId))
            $this->set('userId', $userId);
        $this->loadModel('Product');
        //$this->set('products', $this->Product->find('list', array('fields' => array('id', 'name'))));
        $product = $this->Product->find('all', array('fields' => array('id', 'name')));
        $this->set('products', $product);
          //pr($product); 
    }

知道为什么会发生这种情况或如何阻止它?如果需要,我可以附加数据库表的结构。

1 个答案:

答案 0 :(得分:0)

我想通了,事实证明我实际上并没有将id传递给数据库。根据我的观点 value=" <?php $product['Product']['id']; ?>" 但当我将其更改为value="<?= $product['Product']['id']; ?>"时,系统会正确读取产品ID。我假设我使用的<?php前缀与<?=进行了对比,这是一种错误。