繁体   English   中英

通过单击按钮更新数据库记录,而无需输入

[英]Update database record by click button without input

我试图通过单击按钮来更新我的数据库记录,但是没有任何输入字段。 我想要像静态var这样的东西。

$scope.test = "test"

我想通过单击按钮将此“测试”放入数据库。

例如:

我有数据库表:

ID用户名状态

1个测试打开

在单击“更新”按钮后,我想更新我的数据库记录-从“打开”到“关闭”的状态

当我从输入字段中得到一些信息时,我知道如何更新数据库:

<input type="hidden" ng-model="update_id_user">    
<div class="input-field col s3">
  <input ng-model="update_username" type="text" class="validate" placeholder="Username">
</div>
<div class="input-field col s3">
  <input ng-model="update_state" type="text" class="validate" placeholder="State">
</div>
<div class="input-field col s2">
  <a class="waves-effect waves-light btn blue" ng-click="updateuser()"><i class="material-icons right"></i>Update</a>
</div>

有edituser()和update()

$scope.edituser = function(id_user) {
$http.post('edit.php',{'id_user' : id_user }
 )      
    .success(function (data, status, headers, config) {    


        $scope.update_id_user          =  data[0]["id_user"];
        $scope.update_username        =   data[0]["username"];
        $scope.update_state    =   data[0]["state"];
    })

    .error(function(data, status, headers, config){

    });
}

$scope.updateuser = function() {

    $http.post('update.php', 
                {
                    'id_user'     : $scope.update_id_user,
                    'username'     : $scope.update_username, 
                    'state' : $scope.update_state
                }
              )
            .success(function (data, status, headers, config) {                 
              $scope.getall();
            });
}

edit.php文件

 <?php
require 'conx.php';
$data = json_decode(file_get_contents("php://input"));     
$id_user = $data->id_user; 
$result = $db->query("SELECT * from test WHERE id_user='$id_user'");
$d = array();
while($r = $result->fetch_object())
{
    $d[] = array(
                "id_user" =>  $r->id_user,
                "username" => $r->username,
                "state" => $r->state
                );
}
print_r(json_encode($d));
return json_encode($d);  

update.php文件

<?php
require 'conx.php';
$data = json_decode(file_get_contents("php://input")); 

$id_user = $data->id_user;
$username = $data->username;    
$state = $data->state;
$result = $db->query("UPDATE test SET username='$username' , state='$state'");

主要问题是如何为例如在单击按钮后始终设置为“关闭”的状态设置值。

您可以尝试以下方法:

$scope.edituser = edituser;

$scope.user = {
   'id_user' : $scope.update_id_user
}

function edituser() {
        return $http.post('/edit.php', user).then(function(response) {
            return response.data;
        });
}

暂无
暂无

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

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