按下按钮时的Codeigniter更新表

时间:2015-05-04 16:27:52

标签: php mysql codeigniter

我正在尝试使用Codeigniter创建一个应用程序,当按下按钮时,它会将1添加到表中的INT列。

有3个按钮,根据按下的按钮,它会更新3个列中的一个添加值。

当用户登录时,我已经知道他们登录的用户名和密码被设置到包含输入的表中。

表:

+--+--------+--------+--------+--------+----------+
|ID|Password|Username|inputOne|inputTwo|inputThree|
+--+--------+--------+--------+--------+----------+
|  |        |        |        |        |          |
|  |        |        |        |        |          |
+-------------------------------------------------+

用户登录后应记录

+--+--------+--------+--------+--------+----------+
|ID|Password|Username|inputOne|inputTwo|inputThree|
+--+--------+--------+--------+--------+----------+
|1 |Pass    |User1   |        |        |          |
|  |        |        |        |        |          |
+-------------------------------------------------+

我只需要它来更新列,具体取决于按下哪个按钮。其他列不应添加。直到按下该按钮。

我也尝试这样做,以便在按下按钮时页面不会重定向。

流程:

查看

    <script src="<?php echo site_url('application/views/js/buttons.js')?>"></script>
    <form method="post">
     <input class="btn btn-lg btn-success" type="submit" id="inputOne" value="Button One"><br><br>
     <input class="btn btn-lg btn-primary" type="submit" id="inputTwo" value="Button Two"><br><br>
     <input class="btn btn-lg btn-danger" type="submit" id="inputThree" value="Button One">
   </form>

buttons.js

$(document).ready(function(){

$("#inputOne").click(function()
{
 $.ajax({
     type: "POST",
     url: "<?php echo base_url(); ?>index.php/home/view",
     data: {"1"},
     success:
          function(){
            sleep(5);  //STOP POST
          }
      });
 return false;
});

$("#inputTwo").click(function()
{
 $.ajax({
    type: "POST",
    url: "<?php echo base_url(); ?>index.php/home/view",
    data: {"1"},
    success:
         function(){
           sleep(5);  //STOP POST
         }
     });
 return false;
});

$("#inputThree").click(function()
{
 $.ajax({
   type: "POST",
   url: "<?php echo base_url(); ?>index.php/home/view",
   data: {"1"},
   success:
        function(){
          sleep(5);  //STOP POST
        }
    });
 return false;
});


});

控制器

public function buttons() {
 if (!$this->session->userdata('username')){
  redirect (base_URL(). 'index.php/login/view');
 } else {
  if (!$this->session->userdata('password')) {
   redirect (base_URL(). 'index.php/login/view');
  } else {
   if (date('H:i:s') > $this->session->userdata('endTime')) {
     redirect (base_URL(). 'index.php/login/view');
   } else {
     $this->model->load('buttons_model');
   }
  }
 }
}

模型

<?php

class buttons_model extends CI_Model {


      public function __construct()
  {
          parent::__construct();
  }


public function alterinput() {

    $Username = $this->session->userdata('username');
$Password = $this->session->userdata('password');

$sql = "UPDATE input 
 SET inputOne = (inputOne + 1),
  inputTwo = (inputTwo + 1),
  inputThree = (inputThree + 1)
 WHERE (classPassword = '" . $Username . "',
  Username = '" . $Password . "') ";

      $this->db->query($sql);

  //---------------------------------------
  // END OF FILE
  //---------------------------------------
}

根据按下按钮,我在尝试更新列时出错了什么?

我得到的错误是,当按下按钮一时,我无法单独输入值。只有inputOne应该添加,其余的不应该

作为一个例子,如果我按下按钮一,而我以User1身份登录,则该表现在应该如下所示:

+--+--------+--------+--------+--------+----------+
|ID|Password|Username|inputOne|inputTwo|inputThree|
+--+--------+--------+--------+--------+----------+
|1 |Pass    |User1   |1       |        |          |
|  |        |        |        |        |          |
+-------------------------------------------------+

1 个答案:

答案 0 :(得分:2)

一旦控制器完成并且您想要更新数据库,调用模型和更新方法+ 1,您将需要为执行流程的控制器运行AJAX请求的所有按钮执行一次单击事件。< / p>

<强> JS

$(".btn-lg").on('click', function(e){
    e.preventDefault(); // this will prevent the defualt behavior of the button

    // find which button was clicked
    butId = $(this).attr('id');

    $.ajax({
          method: "POST",
          url: "/controllerDummy/run/",
          data: { button: butId }
        })
      .done(function( msg ) {
        // do something
      });        
});

<强>控制器

// first get the button that was pressed
button = $_POST['button']; // will be the id of the button that was clicked

// do your code here.....

// once completed load the model and run the method of updating...
$this->load->model('button_model');
$this->button_model->methodName();

// if you need it only for specific buttons use an if statement