user3138025 user3138025 - 1 month ago 11
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:

<td>
<select name="adult_fn_list[]" size="3" multiple="multiple">
<?php
$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));
while($row=mysqli_fetch_assoc($res))
echo"<option value=".$row['first_name'].">".$row['first_name']."</option>";
?>
</select>
</td>


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_r($_POST['adult_fn_list']);
print "<br /><br />The value of each individual element is: <br />";
Foreach ( $_POST['adult_fn_list'] as $SelectedFN )
print ($SelectedFN . "<br />");
exit;


Produces the following results:

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

The value of each individual element is:
Leonard
Julia

Answer

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

Comments