简体   繁体   English

按下按钮时的Codeigniter更新表

[英]Codeigniter Update Table when Button pressed

I am trying to use Codeigniter to create an application where when a button is pressed that it'll add 1 to an INT column in a table. 我正在尝试使用Codeigniter创建一个应用程序,在该应用程序中,当按下一个按钮时,它将向表中的INT列添加1。

There is 3 buttons and depending on which button is pressed it'll update one of the 3 columns adding the value. 有3个按钮,根据所按下的按钮,它将更新添加值的3列之一。

I've got so when a user logs in that the username and password they log in with is set into the table with the inputs. 我已经知道,当用户登录时,他们登录时使用的用户名和密码会与输入一起设置到表中。

Table: 表:

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

After the user is logged in this should be recorded 用户登录后,应记录下来

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

I only need it to update the column depending on which button is being pressed. 我只需要它来更新列,具体取决于所按下的按钮。 The other columns should not add. 其他列不应添加。 Until that button is pressed. 直到按下该按钮。

I am also trying to do it so that the page doesn't redirect when the button is pressed. 我也正在尝试这样做,以便在按下按钮时页面不会重定向。

The Process: 流程:

View 视图

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


});

Controller 控制者

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');
   }
  }
 }
}

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
  //---------------------------------------
}

Where am I going wrong in trying to get the columns to update depending on the button press? 在尝试按按钮更新列时,我在哪里出错?

The error I'm getting is that I cannot get the values to enter separately, when Button One is pressed Only inputOne should add and the rest shouldn't 我得到的错误是,当按下按钮1时,我无法获取要分别输入的值,仅应添加inputOne,而不应添加其余部分

As an example, if I pressed Button One whilst I'm logged in as User1 the table should now look like: 例如,如果我以User1身份登录时按了按钮1,则表现在应如下所示:

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

You will need one click event for all buttons that runs an AJAX request to a controller that does your process, once the controller finishes and you want to update the DB, call teh model and the method for updating by + 1. 您将需要一个单击事件,以便所有向运行您的进程的控制器运行AJAX请求的按钮,一旦控制器完成并且您要更新数据库,则调用模型并通过+ 1进行更新。

JS 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
      });        
});

Controller 控制者

// 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM