jjzjj

PHP CodeIgniter-combobox 值未在数据库中更新

coder 2023-10-23 原文

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

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

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

如有任何回复,我们将不胜感激。

我的代码片段如下。

1) Controller

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(); ?>

提前致谢。

最佳答案

使用下面的代码,

$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);

关于PHP CodeIgniter-combobox 值未在数据库中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20297069/

有关PHP CodeIgniter-combobox 值未在数据库中更新的更多相关文章

  1. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby-on-rails - 使用 rails 4 设计而不更新用户 - 2

    我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它​​不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数

  4. ruby-on-rails - 未在 Ruby 中初始化的对象 - 2

    我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调

  5. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  6. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

  7. 世界前沿3D开发引擎HOOPS全面讲解——集3D数据读取、3D图形渲染、3D数据发布于一体的全新3D应用开发工具 - 2

    无论您是想搭建桌面端、WEB端或者移动端APP应用,HOOPSPlatform组件都可以为您提供弹性的3D集成架构,同时,由工业领域3D技术专家组成的HOOPS技术团队也能为您提供技术支持服务。如果您的客户期望有一种在多个平台(桌面/WEB/APP,而且某些客户端是“瘦”客户端)快速、方便地将数据接入到3D应用系统的解决方案,并且当访问数据时,在各个平台上的性能和用户体验保持一致,HOOPSPlatform将帮助您完成。利用HOOPSPlatform,您可以开发在任何环境下的3D基础应用架构。HOOPSPlatform可以帮您打造3D创新型产品,HOOPSSDK包含的技术有:快速且准确的CAD

  8. FOHEART H1数据手套驱动Optitrack光学动捕双手运动(Unity3D) - 2

    本教程将在Unity3D中混合Optitrack与数据手套的数据流,在人体运动的基础上,添加双手手指部分的运动。双手手背的角度仍由Optitrack提供,数据手套提供双手手指的角度。 01  客户端软件分别安装MotiveBody与MotionVenus并校准人体与数据手套。MotiveBodyMotionVenus数据手套使用、校准流程参照:https://gitee.com/foheart_1/foheart-h1-data-summary.git02  数据转发打开MotiveBody软件的Streaming,开始向Unity3D广播数据;MotionVenus中设置->选项选择Unit

  9. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

  10. ruby-on-rails - 创建 ruby​​ 数据库时惰性符号绑定(bind)失败 - 2

    我正在尝试在Rails上安装ruby​​,到目前为止一切都已安装,但是当我尝试使用rakedb:create创建数据库时,我收到一个奇怪的错误:dyld:lazysymbolbindingfailed:Symbolnotfound:_mysql_get_client_infoReferencedfrom:/Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundleExpectedin:flatnamespacedyld:Symbolnotfound:_mysql_get_client_infoReferencedf

随机推荐