1

I have a <select multiple="multiple"> to get multiple choices in php. After that I put it in a variable with the POST method and the I try to at least echo the array out to ensure that my variable$authors has the choices but it prints this error

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\ex\addBook.php on line 45

Here is my code:

<?php
    include ("includes/connections.php");
    $authors = $_POST["authors"];
    foreach ($authors as $author) 
    {
        echo $author;
    }
?>

here is the 'select multiple' I have in html :

<?php multiple("author_ID", "author_firstname", "author_lastname", "author", "author_lastname", "authors"); ?> <span style="font-size: 12px; color: #666;">(Keep down Ctrl for selecting multiple authors)</span>

here is the multiple function:

<?php include ("includes/connections.php");
    function multiple($intIdField, $strfNameField, $strlNameField, $strTableName, $strOrderField, $strNameOrdinal, $strMethod="asc") {
       echo "<select multiple=\"multiple\" name=\"$strNameOrdinal\">\n";

       $strQuery = "select $intIdField, $strfNameField, $strlNameField
                   from $strTableName
                   order by $strOrderField $strMethod";

       $rsrcResult = mysql_query($strQuery);

       while($arrayRow = mysql_fetch_assoc($rsrcResult)) {
          $strA = $arrayRow["$intIdField"];
          $strB = $arrayRow["$strlNameField"] . " " . $arrayRow["$strfNameField"];    
          echo "<option value=\"$strA \">$strB</option>\n";
       }

       echo "</select>";
    }
?>

So I would guess that the $authors variable would be an array and I would happily at least print, but as I already said this does not happen.

The Authors that are taken from the database are showing up correctly without any problem. the thing is I want the ones that are chosen from the 'select multiple' list to be echoed out.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Aki K
  • 1,222
  • 1
  • 27
  • 49
  • The `$authors` array isn't being populated for some reason. You really should check against this condition to keep from getting that error. – Willem Ellis Jun 07 '12 at 18:16
  • how do I populate the '$authors' as an array from a ' – Aki K Jun 07 '12 at 18:19
  • Try the answer on this thread: http://stackoverflow.com/questions/2407284/how-to-get-multiple-selected-values-of-select-box-in-php – Willem Ellis Jun 07 '12 at 18:21
  • I am trying several times but it is not working..maybe I'm declaring the variables in my function wrong or something, I just have no idea – Aki K Jun 07 '12 at 18:29
  • 1
    Can you modify this part of the `multiple()` function? `name=\"$strNameOrdinal\">` If so, make it `name=\"$strNameOrdinal[]\">` – Willem Ellis Jun 07 '12 at 18:34
  • It says 'Notice: Undefined index: authors in C:\xampp\htdocs\ex\addBook.php on line 44' – Aki K Jun 07 '12 at 18:36
  • Obviously, `$strNameOrdinal` doesn't equal `authors` and the array PHP receives has a different index in the `$_POST` array. – Dvir Jun 09 '12 at 19:34
  • Your code is vulnerable to attacks. See [this article](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection). – Gottlieb Notschnabel May 14 '14 at 13:15
  • @GottliebNotschnabel noted. – Aki K May 14 '14 at 14:47

1 Answers1

1

The name of your multiple select in HTML needs to end in '[]' to denote that it is an array, then when you run your foreach loop in php, make sure that it is receiving an array, so it needs to either be set, or you need to typecast a single value as an array. I'm pretty sure your problem is in how you have the name attribute set though.

Ethan
  • 2,754
  • 1
  • 21
  • 34