【问题标题】:PHP CodeIgniter-combobox value not getting update in databasePHP CodeIgniter-combobox 值未在数据库中更新
【发布时间】:2013-11-30 05:50:15
【问题描述】:

我有一些具有编辑和删除选项的产品的表格视图。当我单击编辑时,它会重定向到另一个页面并显示特定数据。有一个名为 产品类别 的字段显示产品类别组合框中的产品,例如比萨饼。

现在,问题开始了。假设我从组合框中选择三明治而不是披萨,并尝试更新数据库中的值。它没有更新任何东西。

我在这个项目中使用了 Codeigniter,所以我想要同样的解决方案。我也在谷歌中搜索过其他帮助,但运气不好。我没有得到正确的解决方案。

任何回复将不胜感激。

我的代码片段如下。

1)控制器

function products_edit($product_id)
{
    $this->load->library('form_validation');
    $this->load->helper('form');  
    $this->load->helper('html');    
    $this->load->model('products_model');
    $data=$this->products_model->general();
    $product = $this->products_model->get_product($product_id);
    $category['categories']=$this->products_model->get_category();
    $this->data1['title'] = 'Edit Product';

    //validate form input
    $this->form_validation->set_rules('name', 'Product name', 'required|xss_clean');
    $this->form_validation->set_rules('description', 'Description', 'required|xss_clean');
    //$this->form_validation->set_rules('category', 'Category', 'required|xss_clean');
    //$this->form_validation->set_rules('extras', 'Extras', 'required|xss_clean');
    $this->form_validation->set_rules('price', 'Price', 'required|xss_clean');
    $this->form_validation->set_rules('is_featured', 'Is Featured', 'required|xss_clean');
    $this->form_validation->set_rules('prorder', 'Order', 'required|xss_clean');

    if (isset($_POST) && !empty($_POST))
    {       
        $data1 = array(
            'product_name'=> $this->input->post('name'),
            'product_desc'=> $this->input->post('description'),
            'product_category'=> $this->input->post('category'),
            'extras'=> $this->input->post('extras'),
            'price'=> $this->input->post('price'),
            'is_featured'=> $this->input->post('is_featured'),
            'prorder' => $this->input->post('order'),
        );

        if ($this->form_validation->run() === true)
        {
            $this->products_model->updateproducts($product_id, $data1);

            $this->session->set_flashdata('message', "<p>Product updated successfully.</p>");

            redirect('products_controller/products_edit/'.$product_id);
        }           
    }

    $this->data1['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));

    $this->data1['product'] = $product;
    //$this->data1['category']=$category;

    //display the edit product form     
    $this->data1['name'] = array(
        'name'      => 'name',
        'id'        => 'name',
        'type'      => 'text',
        'style'     => 'width:300px;',
        'value'     => $this->form_validation->set_value('name', $product['product_name']),
    );

    $this->data1['description'] = array(
        'name'      => 'description',
        'id'        => 'description',
        'type'      => 'text',
        'cols'      =>  40,
        'rows'      =>  5,
        'value'     => $this->form_validation->set_value('description', $product['product_desc']),
    );

        $category1=$this->products_model->get_category();
        $category1['value']=set_value('category',$product['product_category']);
        $temp=array(
        '1'=>$category1['value'],
        );
        $category1=array_diff($category1,$temp);
        $category1['value']=set_value('category',$product['product_category']);

        $this->data1['category']=$category1;



    //$this->data1['category'] = array(
        //'name'    => 'category',
    //  'id'        => 'category',
    //  'type'      => 'text',
    //  'style'     => 'width:300px;',
    //'value'   => $this->form_validation->set_value('category',$category['categories']),set_value('category',$product['product_category']),


        //$this->data1['category']= $this->form_validation->set_value('category',$category['categories']);
        //);


    $this->data1['extras'] = array(
        //'name'    => 'extras',
        //'id'      => 'extras',
        //'type'    => 'text',
        //'style'       => 'width:300px;',
        'value'     => $this->form_validation->set_value('extras', $product['extras']),
    );

    $this->data1['price'] = array(
        'name'      => 'price',
        'id'        => 'price',
        'type'      => 'text',
        'style'     => 'width:40px;text-align: right',
        'value'     => $this->form_validation->set_value('price', $product['price']),
    );

    $this->data1['is_featured'] = array(
        'name'      => 'is_featured',
        'id'        => 'is_featured',
        'type'      => 'text',
        'style'     => 'width:40px;text-align: right',
        'value'     => $this->form_validation->set_value('isfeatured', $product['is_featured']),
    );

    $this->data1['prorder'] = array(
        'name'  => 'prorder',
        'id'    => 'prorder',
        'type'  => 'text',
        'style' => 'width:40px;',
        'value' => $this->form_validation->set_value('prorder', $product['prorder']),
    );
    $this->load->view('products_edit', $this->data1);
}

2)型号

function get_product($product_id) {
    $this->db->select('product_id,product_name,product_desc,product_category,extras,price,is_featured,prorder');
    $this->db->where('product_id', $product_id);
    $this->db->distinct('product_category');
    $query = $this->db->get('product');
    return $query->row_array();

}

function updateproducts($product_id, $data)
{
    $this->db->where('product_id', $product_id);
    $this->db->update('product', $data);
}

3)查看

<?php $product_id = $product['product_id']; ?>
<?php echo form_open("products_controller/products_edit/$product_id");?>
<table width="500" border="1" cellpadding="0" cellspacing="2" align="center">
<tr>
<td width="130" align="right"> Product Name: </td>
<td><?php echo form_input($name); ?></td>
</tr>
<tr>
<td width="130" align="right"> Product Description: </td>
<td><?php echo form_textarea($description); ?></td>
</tr>
<tr>
<td align="right">Product Category:</td>
<td>
<?php echo form_dropdown("product_id",$category,'value')?>      
</td>
</tr>
<tr>
<td align="right">Extras:</td>
<td><?php echo  form_dropdown("product_id",$extras,"#","id='product_id'");?></td>
</tr>
<tr>
<td align="right">Price:</td>
<td><?php echo form_input($price); ?></td>
</tr>
<tr>
<td align="right">Is Featured:</td>
<td><?php echo form_input($is_featured); ?></td>
</tr>
<tr>
<td align="right">Order:</td>
<td><?php echo form_input($prorder); ?></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><?php echo form_submit('submit', 'Submit');?>
</td>
</tr>
</table>
<table align="center">
<tr>
<td>
<?php echo form_close(); ?>
<?php echo form_open("products_controller/products_search");?>
<?php echo form_submit('submit', 'Back');?>
<?php echo form_close(); ?>

提前致谢。

【问题讨论】:

  • @dianuj。感谢您的回复。但是,这对我来说不是正确的解决方案。
  • 你读过你需要那种逻辑的答案的cmets
  • @dianuj,是的朋友,我已经阅读了所有的 cmets。但是,我的问题不同。我想要组合框的解决方案。其他字段值正在更新,但组合框值未更新。

标签: php mysql codeigniter combobox


【解决方案1】:

使用下面的代码,

$test = array();
$count=count($category['categories']);
for($i=0;$i<$count;$i++)
{
 $test[$i] = array($category['categories'][$i] => $category['categories'][$i]);
} 
$this->data['category']=set_value('category',$test);

【讨论】:

    【解决方案2】:

    获取&lt;?php echo form_dropdown("product_id",$category,'value')?&gt;的值 您必须写 $this-&gt;input-&gt;post('product_id') 这将为您获取产品 ID,然后您可以使用它来更新数据库

    【讨论】:

    • 但是朋友其他值正在更新,只有组合框值没有。
    • 我已经浏览过你的代码好几次了。我可以看到您正在通过 [link]'product_category'=> $this->input->post('category') 访问您的 product_category 值,但我没有找到任何名为 'category' 的下拉列表,请检查一下。谢谢
    • $this->data1['category']=$category1;此语句是名为“类别”的组合框
    猜你喜欢
    • 2020-06-11
    • 1970-01-01
    • 2021-03-27
    • 2015-07-09
    • 1970-01-01
    • 2016-02-13
    • 2013-09-28
    • 1970-01-01
    • 2019-06-24
    相关资源
    最近更新 更多