Php 避免重复mysql注入

Php 避免重复mysql注入,php,mysql,angularjs,Php,Mysql,Angularjs,我创建了一个控制器并调用了一次该函数。但它调用了两次并插入了两次值。我在控制器中调用了服务upload_album。现在插入了两次值。一次是原始值,另一次是伪值 Controller $scope.savealbum = function() { $scope.album_pids = $routeParams.id; $timeout(function () { //console.log($scope.justapp);

我创建了一个控制器并调用了一次该函数。但它调用了两次并插入了两次值。我在控制器中调用了服务upload_album。现在插入了两次值。一次是原始值,另一次是伪值

Controller
       $scope.savealbum = function() {
        $scope.album_pids = $routeParams.id;

        $timeout(function () {
        //console.log($scope.justapp);
        for (tt in $scope.justapp) {
            if ($scope.justapp[tt].id == $scope.album_pids) {
                for (var i = 0; i < $rootScope.media_lib.length; i++) {

                }
            }
        }            
         $scope.promise=AlbumServices.upload_album($scope.album_pids,$scope.images,$scope.videos);
          $scope.promise.then(function(data) {
            console.log(data);
            alert('Photos Added to Album Successfully');
           // $location.path('album_details/' + $routeParams.id);
        }, function(reason) {
            console.log(reason);
        });
        }, 1500, false);
    };
 Service
   upload_album: function (alb,img,vid) {
            var deferred = $q.defer();
            var data = {};
            data.pagename = "upload_album";
            data.album = alb;
            data.photo = img;
            data.video = vid;
            $http.post('js/data/album.php', data)
                    .success(function (data, status, headers, config)
                    {
                        console.log(status + ' - ' + data);
                        deferred.resolve(data);
                    })
                    .error(function (data, status, headers, config)
                    {
                        deferred.reject(data);
                        console.log('error');
                    });
            return deferred.promise;
        }

       php

       function upload_album ($prefix) {
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$sub_id=$request->album;
$val=$request->photo;
$photo = json_encode($val);
$video = json_encode($request->video);

 $now = date('Y-m-d H:i:s');
$count_pho = sizeof($photo);
 $count_vid = sizeof($video);
$test = '';
if($count_pho != '0' ) { 
    $test .= "('".$sub_id."','".$content_type."','".$photo."','".$website_id."','".$now."'),";        
    $demo = substr($test, 0, -1);
    $sql="INSERT INTO `album_details` (SUB_ID,CONTENT_TYPE,CONTENT_VALUE,WEBSITE_ID,CreatedTime)VALUES".$demo;
    $query = mysql_query($sql) or sqlerrorhandler("(".mysql_errno().") ".mysql_error(), $sql, __LINE__);
}


if ($query) {        
    echo $msg = true;   
} else {
    echo $msg = false;
}  
控制器
$scope.savealbum=函数(){
$scope.album_pids=$routeParams.id;
$timeout(函数(){
//log($scope.justapp);
for(tt在$scope.justapp中){
if($scope.justapp[tt].id=$scope.album_-pids){
对于(变量i=0;i<$rootScope.media_lib.length;i++){
}
}
}            
$scope.promise=AlbumServices.upload_相册($scope.album_-pids,$scope.images,$scope.videos);
$scope.promise.then(函数(数据){
控制台日志(数据);
警报(“照片已成功添加到相册”);
//$location.path('album_details/'+$routeParams.id);
},功能(原因){
控制台日志(原因);
});
},1500,假);
};
服务
上传相册:功能(alb、img、vid){
var deferred=$q.deferred();
变量数据={};
data.pagename=“上传相册”;
data.album=alb;
data.photo=img;
data.video=vid;
$http.post('js/data/album.php',data)
.success(函数(数据、状态、标题、配置)
{
console.log(状态+'-'+数据);
延迟。解析(数据);
})
.error(函数(数据、状态、标题、配置)
{
延迟。拒绝(数据);
console.log('error');
});
回报。承诺;
}
php
函数上传相册($prefix){
$postdata=文件\u获取\u内容(“php://input");
$request=json_decode($postdata);
$sub_id=$request->album;
$val=$request->photo;
$photo=json_encode($val);
$video=json_encode($request->video);
$now=日期('Y-m-d H:i:s');
$count_pho=sizeof($photo);
$count_vid=sizeof($video);
$test='';
如果($count_pho!=“0”){
$test.=“(“$sub_id.”、“$content_type.”、“$photo.”、“$website_id.”、“$now.”、”;
$demo=substr($test,0,-1);
$sql=“插入“相册详细信息”(子ID、内容类型、内容值、网站ID、CreatedTime)值”。$demo;
$query=mysql\u query($sql)或sqlerrorhandler(“.mysql\u errno()”).mysql\u error(),$sql,\uuuuuuuuuuuuuuu行);
}
如果($query){
echo$msg=true;
}否则{
echo$msg=false;
}  

}因为我们无法使用整个代码(包括HTML),我的建议如下:

  • 在angular中检查html和/或run方法,确保控制器没有实例化两次
  • 在数据库中创建唯一的密钥对(这可能有助于避免出现双记录)
  • 在使用timeout时创建一个debouncer,以确保超时总是启动一次。大概是这样的:

    var t = null;
    var mySaveFunction = function () {
        if (t) {
            clearTimeout(t);
        }
        t = setTimeout(function () {
            /* do saving here */
        }, 2000);
    };
    

  • 所以这基本上与mysql无关