dovt85093 2012-03-04 19:00
浏览 26
已采纳

使用CodeIgniter循环发布数据并更新/保存到DB

I have a page where you can update a lot of data, I'm wondering what the best way to store this data on the backend is (using the least amount of code generally).

The data set on the page where each value can be changed would be something like this:

   ID NAME   DATA MURDATA FINALDATA
   1  MyName XYZ  425     Blah
   2  Anoth  ASDF 87      987
   3  Last   DKL  38      19bm

How do I loop through the post data so I can do updates on all this submitted data at once?

  • 写回答

1条回答 默认 最新

  • douli0531 2012-03-04 19:18
    关注

    You can't update two tuples with different values. You will have to do one UPDATE for each tuple or do a bulk insert using the ON DUPLICATE KEY UPDATE-syntax. See the MySQL reference.

    Have a form with inputs that have [] as suffix to their names. This way you can access these inputs as an array in PHP:

    $result = mysql_query("SELECT * FROM table");
    while ($tuple = mysql_fetch_assoc($result))
    {
        echo '<input type="text" name="name['.(int)$tuple["id"].']" value="'.htmlentities($tuple["name"]).'" />';
        // echo more inputs ...
    }
    

    Your post processing code could look like this

    foreach ($_POST["name"] as $id => $data)
    {
        // todo: check if all $_POST columns are set, before accessing them
        // if (!isset($_POST["data"][$id], $_POST["murdata"][$id], ...)
        //     continue;
    
        $query = "UPDATE table SET ".
        $query .= "name = ".mysql_real_escape_string($_POST["name"][$id])."'";
        // the other columns...
        $query .= "WHERE id = '".(int)$id."'";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 nslt的可用模型,或者其他可以进行推理的现有模型
  • ¥15 arduino上连sim900a实现连接mqtt服务器
  • ¥15 vncviewer7.0安装后如何正确注册License许可证,激活使用
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并2
  • ¥66 关于人体营养与饮食规划的线性规划模型
  • ¥15 基于深度学习的快递面单识别系统
  • ¥15 Multisim仿真设计地铁到站提醒电路
  • ¥15 怎么用一个500W电源给5台60W的电脑供电
  • ¥15 请推荐一个轻量级规则引擎,配合流程引擎使用,规则引擎负责判断出符合规则的流程引擎模板id
  • ¥15 Excel表只有年月怎么计算年龄