产品类别 ID 不适用于 CodeIgniter 中的产品表单

Product category id is not working on product form in CodeIgniter

我正在制作一个网站,以便用户可以向该网站添加产品。但是当我 select 我的产品表单视图文件中的某个类别并上传产品时,没有为产品提供类别 ID,它说类别 ID = 0。

Some database information:

categories table:
Row 1: id
Row 2: name

In the products table:
Row: category_id

这是我的视图表单文件中类别的下拉菜单:

 <select name="category_id">
        <?php foreach (get_categories_h() as $category) : ?>
            <option><?php echo $category['name']; ?></option>
        <?php endforeach; ?>
        </select>

这是我的表单控制器中数据库插入功能的一部分:

$this-> db-> insert('products', array(
                'product_foto' => 'new_'.$data["raw_name"].$data['file_ext'],
                'product_foto_thumb' => 'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'],
                'product_naam' => $this->input->post('product_naam'),
                'product_beschrijving' => $this->input->post('product_beschrijving'),
                'ophaal_plaats' => $this->input->post('ophaal_plaats'),
                'category_id' => $this->input->post('category_id'),
                'date_created' => date('Y-m-d'),
                'date_updated' => date('Y-m-d')
            );

希望有人能帮帮我,谢谢

试试这个

<select name="category_id">
        <?php foreach (get_categories_h() as $category) : ?>
            <option value="<?=$category['id'];?>"><?php echo $category['name']; ?></option>
        <?php endforeach; ?>
</select>

您可以尝试此解决方案来解决您的问题。

Change your view file

<select name="category_id">
        <?php foreach (get_categories_h() as $category) : ?>
            <option value="<?=$category['id'];?>"><?php echo $category['name']; ?></option>
        <?php endforeach; ?>
</select>

Changes your controller file.

$this-> db-> insert('products', array(
                'product_foto' => 'new_'.$data["raw_name"].$data['file_ext'],
                'product_foto_thumb' => 'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'],
                'product_naam' => $this->input->post('product_naam'),
                'product_beschrijving' => $this->input->post('product_beschrijving'),
                'ophaal_plaats' => $this->input->post('ophaal_plaats'),
                'category_id' => !empty($this->input->post('category_id')) ? $this->input->post('category_id') : 0,
                'date_created' => date('Y-m-d'),
                'date_updated' => date('Y-m-d')
            );

希望对您有所帮助。