user3138025 user3138025 - 1 year ago 62
HTML Question

Why do I receive a warning error when passing an array

I receive the following warning notice when passing an array from the HTML to the PHP:

Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in (.../post2.php on line 68)

Here's where the data are captured in the input2.php file:

<select name="adult_fn_list[]" size="3" multiple="multiple">
$adult_sql = "SELECT first_name FROM member
WHERE family_fkey = 34
AND member_type = 'Adult'
ORDER BY prim_key";
$res=mysqli_query($link, $adult_sql) or die (mysqli_error($link));
echo"<option value=".$row['first_name'].">".$row['first_name']."</option>";

Here's line 68 in the post2.php code

$adult_fn_list = mysqli_real_escape_string($link, $_POST[adult_fn_list]);

I don't know why I'm receiving the warning notice. When I print the array and elements out, they appear fine. The following code:

print "The value of the adult_fn_list is: ";
print "<br /><br />The value of each individual element is: <br />";
Foreach ( $_POST['adult_fn_list'] as $SelectedFN )
print ($SelectedFN . "<br />");

Produces the following results:

The value of the adult_fn_list is: Array ( [0] => Leonard [1] => Julia )

The value of each individual element is:

Answer Source

You are posting HTML array: name="adult_fn_list[]", not a string. Therefore you can't pass it to mysqli_real_escape_string().