gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:49:05 25 4
gpt4 key购买 nike

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

现在,当我将数据提交到数据库时,如果输入为空,我的代码会用 0 更新数据库中的该列,并丢失以前的数据。如何解决这个问题?如果输入为空,则不向数据库发送任何数据。

<?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 Controller

 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>

最佳答案

您应该发送单个对象,而不是在 $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,或者是否发送 null 并且服务器正在将 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));

如果每次都设置值而不检查是否有值,那么即使该值为空,您也总是会丢失之前的数据(因此更新后的值将为空)。

关于php - 更新数据库时我不想更新所有列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40878644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com