1

I have trouble sanitizing my array and am hoping someone can take a look!

here is my input:

<input type="text" name="courseno[]" id="courseno" size="12" />

here is my function to sanitize my data:

function sanitizeData ($datastring) {
    if (is_array($datastring)) {
        foreach ($datastring as $indivdata) {
            $indivdata = sanitizeData($indivdata);
        }
    }
    else {
        $datastring=trim($datastring);
        $datastring=htmlspecialchars($datastring);
        $datastring = mysql_real_escape_string($datastring);
        return $datastring;
    }
}

if (isset($_POST['courseno'])) {
    $courseno = sanitizeData($_POST['courseno']);
}

the $courseno data won't post when I try to sanitize the array, while all my other data gets posted. When I don't sanitize $courseno, the data gets posted to the database just fine.

1
  • Normally, the data is sanitized after it is posted, ie in the server, how are you trying to sanitize it and then post?? Commented Jun 21, 2011 at 5:27

4 Answers 4

1

Well, it would help if sanitizeData() returned a value when $datastring is an array.

if (is_array($datastring)) {
    foreach ($datastring as $indivdata) {
        $indivdata = sanitizeData($indivdata);
    }
    // you need to actually return something here
}
Sign up to request clarification or add additional context in comments.

Comments

1

In your if (is_array($datastring)) test, you assign the output of the sanitizeData call back to $indivData. So if the input contains an array, a real value is never returned. $courseno will be assigned a NULL value. You'd probably want to change it to something along the lines of this:

function sanitizeData ($datastring) {
    if (is_array($datastring)) {
        $result = '';
        foreach ($datastring as $indivdata) {
            $result .= sanitizeData($indivdata);
        }
    return $result;   
}
else {
    $datastring=trim($datastring);
    $datastring=htmlspecialchars($datastring);
    $datastring = mysql_real_escape_string($datastring);
    return $datastring;
  }
}

if (isset($_POST['courseno'])){
    $courseno = sanitizeData($_POST['courseno']);
}

Comments

0

It is just a variable scope problem.

function sanitizeData ($datastring) {
    // This is needed to hold the value between function calls ...        
    static $indivdata = array();         

    if (is_array($datastring)) {
       foreach ($datastring as $indivdata) {
          $indivdata[] = sanitizeData($indivdata);
       }
       return $indivdata;
   } 
   else {
     $datastring=trim($datastring);
     $datastring=htmlspecialchars($datastring);
     $datastring = mysql_real_escape_string($datastring);

     return $datastring; 
     } 
  }   
 // $courseno will be an array now.
 if (isset($_POST['courseno'])){
       $courseno= sanitizeData($_POST['courseno']);
 }

Unless I missed something at this late hour, it seems your function does not return the data array. There are a couple of ways to solve this, but the above code should get you in the right track.

1 Comment

Thank you for your response! I tried this out and came up with this error: "[] operator not supported for strings in
0

I think you have put your text box outside the form tag.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.