首页 > 解决方案 > 表单提交更新数据库记录后,HTML文本框未更新

问题描述

我正在使用 CodeIgniter。在应用程序/视图/abc.php

<input type="text" name="company_profit" id="company_profit" value="<?php echo $form_data['company_profit']; ?>" >

在应用程序/控制器/Cde.php

$data['form_data'] = $form_data = $this->common->getOneRow("tbl_psb_setting","WHERE id=1");

if(isset($_POST) && count($_POST) > 0)
{   
    if($this->form_validation->run())       
    {   
        $up_data['company_profit'] = $this->input->post('company_profit');
        $this->common->updateRecord('tbl_psb_setting',$up_data,"id=1"); 
    }
}

当我单击提交按钮时,html 表单提交,数据库记录成功更新。但是为什么 HTML 文本框“company_profit”的值没有更新?我需要按 F5 刷新页面,然后才能看到文本框值已更新。

当我单击“提交”按钮时,我可以看到页面正在刷新,这意味着 html 表单提交了自动刷新页面,所以我猜在新记录更新到数据库之前,mysql 从分配给文本框的数据库中检索数据太快了?所以我尝试代码 php sleep(3); 等待 3 秒然后才开始从数据库中检索数据分配到文本框值,但失败,文本框仍然显示旧值。任何的想法?

标签: phphtmlmysql

解决方案


您在更新数据之前获取数据。只需像这样移动下面的 fetch :

if(isset($_POST) && count($_POST) > 0)
{   
    if($this->form_validation->run())       
    {   
        $up_data['company_profit'] = $this->input->post('company_profit');
        $this->common->updateRecord('tbl_psb_setting',$up_data,"id=1"); 
    }
}

$data['form_data'] = $form_data = $this->common->getOneRow("tbl_psb_setting","WHERE id=1");

推荐阅读