更新数据库时,我不想更新所有列

时间:2016-11-30 01:15:55

标签: php mysql angularjs

更新数据库时,我不想更新所有列,只有在我填写了<input/>

时才更新

现在使用我的代码,如果输入为空,我将数据提交到数据库,我在db中更新了该列,其中包含0并丢失了当前数据。如何解决?如果输入为空,则不要向db发送任何数据。

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$mysql_host = "localhost";
$mysql_username = "root";
$mysql_password = "";
$mysql_database = "medvedgrad";

// First we create the connection
$pdo = new PDO("mysql:host=".$mysql_host .";dbname=".$mysql_database.";charset=utf8", $mysql_username, $mysql_password);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  

$data = json_decode(file_get_contents("php://input"));
$zm = $data->zlatni_medvjed;
$ck = $data->crna_kraljica;
$gv = $data->gricka_vjestica;
$dk = $data->dva_klasa;
$fk = $data->fakin;

// Then we prepare, and execute the query
$stmt = $pdo->prepare("UPDATE `stanje_piva` SET `zlatni_medvjed`=`zlatni_medvjed`+:zm, `crna_kraljica`=`crna_kraljica`+:ck, `gricka_vjestica`=`gricka_vjestica`+:gv, `dva_klasa`=`dva_klasa`+:dk,`fakin`=`fakin`+:fk WHERE `id`=1");
$stmt->execute(array("zm" => $zm, "ck" => $ck, "gv" => $gv, "dk" => $dk, "fk" => $fk));
?>

这是我的角度控制器

 angular.module('angularLoginApp')
.controller('UnosUSkladisteCtrl', function($scope,$http) {

    $scope.insertdata = function(){
        $http.post("database/unos-piva.php", {'zlatni_medvjed':$scope.zlatni_medvjed, 'crna_kraljica':$scope.crna_kraljica, 'gricka_vjestica':$scope.gricka_vjestica, 'dva_klasa':$scope.dva_klasa, 'fakin':$scope.fakin })
        .success(function(data,status,headers,config){
            console.log("Podaci uspiješno spremljeni");
            alert("Nove količine piva su dodane u skladište");
        });
    }

    $scope.data = {message: "Molimo vas da točno navedete što unosite u skladište"};

});

HTML

 <form class="form-horizontal col-xs-12" col-sm-12" name="signUpForm" ng-submit="submitFormSignUp()" novalidate>

                <!-- Zlatni medvjed -->
                <div class="form-group" ng-class="">
                    <label class="col-sm-4 col-xs-12 control-label no-padding-right " for="zlatni_medvjed">Zlatni medvjed boca 0.5l</label>
                    <div class="col-sm-4 col-xs-12">
                        <span class="block input-icon input-icon-right">  
                            <input ng-model="zlatni_medvjed" ng-change="doCheck()" placeholder="Količina boca 0.5l" type="number"  class="form-control">                        
                        </span>   
                    </div>
                </div>
                <!-- Crna kraljica -->
                <div class="form-group" ng-class="">
                    <label class="col-sm-4 col-xs-12 control-label no-padding-right " for="crna_kraljica">Crna kraljica boca 0.5l</label>
                    <div class="col-sm-4 col-xs-12">
                        <span class="block input-icon input-icon-right">  
                            <input ng-model="crna_kraljica" placeholder="Količina boca 0.5l" type="number"  class="form-control">                        
                        </span>   
                    </div>
                </div>
                <!-- Grička vještica -->
                <div class="form-group" ng-class="">
                    <label class="col-sm-4 col-xs-12 control-label no-padding-right " for="gricka_vjestica">Grička vještica boca 0.5l</label>
                    <div class="col-sm-4 col-xs-12">
                        <span class="block input-icon input-icon-right">  
                            <input ng-model="gricka_vjestica" placeholder="Količina boca 0.5l" type="number"  class="form-control">                        
                        </span>   
                    </div>
                </div>
                <!-- Dva klasa -->
                <div class="form-group" ng-class="">
                    <label class="col-sm-4 col-xs-12 control-label no-padding-right " for="dva_klasa">Dva klasa boca 0.5l</label>
                    <div class="col-sm-4 col-xs-12">
                        <span class="block input-icon input-icon-right">  
                            <input ng-model="dva_klasa" placeholder="Količina boca 0.5l" type="number"  class="form-control">                        
                        </span>   
                    </div>
                </div>
                <!-- Fakin -->
                <div class="form-group" ng-class="">
                    <label class="col-sm-4 col-xs-12 control-label no-padding-right " for="fakin">Fakin boca 0.5l</label>
                    <div class="col-sm-4 col-xs-12">
                        <span class="block input-icon input-icon-right">  
                            <input ng-model="fakin" placeholder="Količina boca 0.5l" type="number"  class="form-control">                        
                        </span>   
                    </div>
                </div>

                <!-- SUBMIT BUTTON -->
                <label class="col-sm-4 control-label no-padding-right"></label>
                    <div class="col-sm-4">
                        <button ng-click="insertdata()" type="submit" class="btn btn-primary btn-lg btn-block">Unesi količine u skladište</button>
                    </div>
    </form> 

1 个答案:

答案 0 :(得分:0)

您应发送单个对象,而不是手动设置$ http.post函数中对象的每个属性。

angular.module('angularLoginApp')
.controller('UnosUSkladisteCtrl', function($scope,$http) {
$scope.form = {};

$scope.insertdata = function(){
    $http.post("database/unos-piva.php", $scope.form)
    .success(function(data,status,headers,config){
        console.log("Podaci uspiješno spremljeni");
        alert("Nove količine piva su dodane u skladište");
    });
}

$scope.data = {message: "Molimo vas da točno navedete što unosite u skladište"};

});

然后在你的表单中使用html制作表格对象的ng-model =“”访问属性,如下所示:

<input ng-model="form.zlatni_medvjed" ng-change="doCheck()" placeholder="Količina boca 0.5l" type="number"  class="form-control">

当您这样做时,您应该将空值发送到服务器。 您还可以在网络选项卡上检查开发控制台是否真的向服务器发送0,或者它是否发送空并且服务器将0保存到数据库。

修改

问题在于:

//Then we prepare, and execute the query
$stmt = $pdo->prepare("UPDATE `stanje_piva` SET `zlatni_medvjed`=`zlatni_medvjed`+:zm, `crna_kraljica`=`crna_kraljica`+:ck, `gricka_vjestica`=`gricka_vjestica`+:gv, `dva_klasa`=`dva_klasa`+:dk,`fakin`=`fakin`+:fk WHERE `id`=1");
$stmt->execute(array("zm" => $zm, "ck" => $ck, "gv" => $gv, "dk" => $dk, "fk" => $fk));

如果您每次都设置值而不检查是否有值,那么即使值为null(因此更新的值为null),您也将始终丢失先前的数据。