php - Codeigniter 更新特定值

标签 php mysql codeigniter

<分区>

我在 MySql 数据库中有一个表,该表具有 post_id、post_number、post_like

我在 codeigniter 中使用 MVC 从 mysql 表中获取所有数据并将其显示在页面上(这部分已完成)

我需要为每个 post_id 添加一个按钮,将 +1 添加到 mysql 表中的 post_like,对应于 post_id。

因此,如果我点击 post_id 7 的 +1 按钮,post_like 应该只会增加 post_id 7。

这是我目前所拥有的!

view_page.php

     <?php foreach($members as $value): ?>
        <tr>
          <td><?php echo $value['post_id']; ?></td>
          <td><?php echo $value['post_like']; ?></td>
          <td> <a href="plusone/<?php echo $value['post_id']; ?>">Add +1</a></td>
        </tr>
      <?php endforeach; ?>

controller_page.php

   <?php


    function plusone($post_id){

       $this->load->model('model_page');
       $this->model_page->data_addition($post_id);
       redirect('controller_page/viewdata');

   }

   ?>

Model_page.php

        <?php

    function data_addition($post_id){

       //trying to add 1 the post_like and update it in the db
        $add_one_to_data = $post_like+ 1;

        $data = array(
           'post_like' => $add_one_to_data +1 
        );

        $this->db->where('post_id', $post_id);
        $this->db->update('post_tbl', $data); 

    }

   ?>

请不要笑我在数组上面添加的$add_one_to_data,我不知道还能做什么

提前致谢

最佳答案

您需要先获取 ifno,例如:

function data_addition($post_id){
       $post_like = $this->db->get_where('post_tbl', array('post_id' => $post_id))->row();

       //trying to add 1 the post_like and update it in the db
        $add_one_to_data = (int) $post_like->post_like + 1;

        $data = array(
           'post_like' => $add_one_to_data +1 
        );

        $this->db->where('post_id', $post_id);
        $this->db->update('post_tbl', $data); 

    }

关于php - Codeigniter 更新特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45358082/

相关文章:

mysql - 从某一行开始将整数字段递增 1

javascript - 如何使用JSON和PHP保存数据?

php - 每两分钟将值插入数据库表中

MySql:如何从一个数据库读取表,同时每行搜索第二个数据库表?

php - 分页在 codeigniter 中不起作用

php - 由 Codeigniter 支持的 jQuery DataTables

php - Codeigniter 在一个函数中使用三个查询

php - .htaccess 重写 : subdomain as GET var and path as GET var

php - 如何让 Apache 输出真正的所有错误?

php - 在 PHP 或其他任何程序中执行时间限制函数