-2

I have this php code for html form

if(isset($_POST['job_title'])){
  foreach($_POST['job_title'] as $selected) {
     $job_title[] = $selected ;
  }
  $job_title = json_encode($job_title);
  $job_title = clean_string($job_title);
}

and the this is what clean_string function code which cleans input for SQL

function clean_string($string){
   global $connection;
   $string = trim($string);
   $string = stripslashes($string);
   $string = htmlspecialchars($string);
   $string = htmlentities($string);
   $string = mysqli_real_escape_string($connection,$string);
   return $string;
}

So when this code execute it cause an error like( expects parameter 1 to be string, array given)

How to solve this problem ? Thanks in advance

2 Answers2

0

Uncle Sam might probably argue that this is what you are most likely intent on doing:

<?php

    if(isset($_POST['job_title'])){
        foreach($_POST['job_title'] as $selected) {
            $job_title[] = clean_string($selected);
        }
        $job_title  = json_encode($job_title);
    }


    function clean_string($string){
        global $connection;
        $string = trim($string);
        $string = stripslashes($string);
        $string = htmlspecialchars($string);
        $string = htmlentities($string);
        $string = mysqli_real_escape_string($connection,$string);
        return $string;
    }

Alternative II: Using array_map()

<?php
    if(isset($_POST['job_title'])){
        foreach($_POST['job_title'] as $selected) {
            $job_title[] = $selected;
        }
        $job_title  = json_encode(array_map("clean_string", $job_title) );
    }


    function clean_string($string){
        global $connection;
        $string = trim($string);
        $string = stripslashes($string);
        $string = htmlspecialchars($string);
        $string = htmlentities($string);
        $string = mysqli_real_escape_string($connection,$string);
        return $string;
    }
Poiz
  • 7,611
  • 2
  • 15
  • 17
  • Yes the code was like this before , But I thought its preferable to put it after using `json_encode` ! So is it enough to use it before `json_encode` ?? @Poiz – Mustafa Alsuhaibi Aug 20 '16 at 14:50
  • @MustafaAlsuhaibi Absolutely... if you need to use it on an Array, you could follow the updated post that uses array_map() – Poiz Aug 20 '16 at 14:58
0

From what you've posted, it looks like the custom function clean_string($string) accepts a string parameter and returns a string.

And you have an array $job_title which needs to be sanitized.

The problem you're facing is you're passing a JSON to clean_string($string) in this line:

  $job_title = json_encode($job_title);
  $job_title = clean_string($job_title);  // JSON object is passed.

So, you simply need to traverse each element through the array $job_title and keep passing each value to clean_string(). This can be achieved using array_map().

if (isset($_POST['job_title'])) {
    foreach ($_POST['job_title'] as $selected) {
        $job_title[] = $selected ;
    }
    $job_title = array_map("clean_string", $job_title);  // Modify this line
    $job_title = json_encode($job_title);  // Add it here if you need to json encode the sanitized input
}
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32