避免重复的mysql注入

时间:2015-12-03 11:15:59

标签: php mysql angularjs

我创建了一个控制器并且调用了一次该函数。但是它调用了两次并且插入了两次值。我在controller中调用了服务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;
}  

}

1 个答案:

答案 0 :(得分:2)

因为我们无法查看整个代码(包括HTML),我的建议是:

  1. 检查你的html和/或在angular内部运行方法,以确保你的控制器没有实例化两次
  2. 在您的数据库中创建一个唯一的密钥对(它可能不会有双重记录)
  3. 在使用超时时创建去抖动器,以便在超时总是启动一次时。像这样的东西:

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