0

Warning : mysqli_real_escape_string() expects parameter 2 to be string, array given in C:\xampp\htdocs\nav\php\pinsert.php

I get the above error when I try to post array to php using http.post in angularjs. What is the code to send array to php and insert it into database.

app.js

     $scope.saveRecord = function (newProduct)
    {
      $scope.albumNameArray = []; 

    angular.forEach($scope.check, function(album){

            if (album.select)  $scope.albumNameArray.push(album.name);

        });

       $http.post("php/pinsert.php",{
                     'names' : $scope.newProduct.name,
                     'catg' : $scope.newProduct.catg,
                     'stat' : $scope.newProduct.stat,
                      'tag' : $scope.albumNameArray
              })  

PHP

     <?php 


   $connect = mysqli_connect("localhost", "root", "","user");
   $data = json_decode(file_get_contents("php://input"));


  $p_name = mysqli_real_escape_string($connect, $data->names);
  $p_catg = mysqli_real_escape_string($connect, $data->catg);
  $status = mysqli_real_escape_string($connect, $data->stat);
  $tag = mysqli_real_escape_string($connect, $data->tag);
   $query = "INSERT INTO products(pname,pcatg,status,tag)  VALUES ('$p_name','$p_catg','$status','$tag')";

    $result = mysqli_query($connect, $query) ;

  if($result == TRUE) 
   {  
       echo "Data Inserted...";  
  }  
  else  
  {  
       echo 'Error';  
  }  

  ?>
6
  • The error says that you passing an array as second parameter to mysqli_real_escape_string() instead of a String. Commented May 26, 2017 at 10:22
  • tats my queestion.. what is the code to send array to php using angularjs Commented May 26, 2017 at 10:24
  • If you are passing an array to the function, it means that you received an array. That is why I thought you didn't understand the error message. Commented May 26, 2017 at 10:29
  • print_r($data) Commented May 26, 2017 at 10:31
  • try accessing the data in $data['names'] fashion. Commented May 26, 2017 at 10:32

2 Answers 2

0

try with json data.

var data = { 'names' : $scope.newProduct.name, 'catg' : $scope.newProduct.catg, 'stat' : $scope.newProduct.stat, 'tag' : $scope.albumNameArray };
 $http({ url: 'php/pinsert.php',
            method: "POST",
            data: JSON.stringify(data),
            headers: {'Content-Type': 'application/json'}
        }).then(function(reponse){
          }) 
Sign up to request clarification or add additional context in comments.

Comments

0

I am using this way

$scope.myArray = [14742,  14744,  22781,  15540 ];

And $http request need to look like this: as you can see 'parameter[]' ... is array type. Else you need to JSOM.strignify(data) ... then json_decode in php.

$scope.saveSomething = function(){
        $http({
            method: 'post',
            url: '/some-url',
            params:{
                'parameter[]': $scope.myArray,
            }
        });
    };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.