1

Hi I'm using foreach to insert into my database multiple authors for a book.

Here is the foreach:

foreach ($_POST["authors"] as $author) 
{
    $sqlAuthor = "INSERT INTO book_author (book_ISBN, author_ID)
        VALUES('$isbn','$author')";

    mysql_query($sqlAuthor);
}

I am getting the authors with a select multiple from my page for which I have created a function to display in the select multiple the authors available.

The code is producing exactly what I want it to produce in the page from my database (the names of all the authors)

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>";
    }
?>

Now for some reason it throws the following errors:

Notice: Undefined index: authors in C:\xampp\htdocs\ex\addBook.php on line 63

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

I think it tells me that it can't find the authors array but I find it very strange

Here the php code inside the html to call the multiple() function:

<?php multiple("author_ID", "author_firstname", "author_lastname", "author", "author_lastname", "authors"); ?>

Anyone has any idea what is wrong with my code?

3
  • 1
    Please stop writing new code with the ancient MySQL extension: it is no longer maintained and the community has begun the deprecation process; you can use instead either the improved MySQLi extension or the PDO abstraction layer. Commented Jun 7, 2012 at 19:02
  • 1
    Your code is vulnerable to SQL injection. You really should be using prepared statements, into which you pass your variables as parameters that do not get evaluated for SQL. If you don't know what I'm talking about, or how to fix it, read the story of Bobby Tables. Commented Jun 7, 2012 at 19:04
  • 1
    Have you looked at the HTML that your code is generating to make sure it's producing what you expect? Commented Jun 7, 2012 at 19:07

4 Answers 4

2

Change to

echo "<select multiple=\"multiple\" name=\"{$strNameOrdinal}[]\">\n";

and also check if its set before using it as others suggested.

Edit: Not related to the issue on hand but you have an extra space after $strA, change to if it wasn't on purpose.

echo "<option value=\"$strA\">$strB</option>\n";
Sign up to request clarification or add additional context in comments.

3 Comments

I tried it and it did not work, also I left the if out on purpose and even after choosing authors it still show the error that "authors" is undefined. So even if I choose the authors it is still not defined. Very weird
I suggest you use validator.w3.org to validate your html and then firebug (or any alternative) to check what's being posted.
I left the if statement out, that way it will show an error when the array is empty even if it should be not
1

Every time you use foreach nest it in an if:

if (count($_POST["authors"])) {
    foreach ($_POST["authors"] as $author)  {
       ...
    }
}

so if you have 0 element in the array, or it's not an array (string, null, anything else) you won't enter the if, and won't get the error on foreach's wrong parameter

8 Comments

the if just checks for empty array, my problem is that even if I select the authors I want it is still an empty array
@SilliconTouch, what does var_dump($_POST["authors"]) display when you have 2 elements selected and posted?
it displays this: array(2) { [0]=> string(3) "10 " [1]=> string(2) "1 " }
Jesus! I found it, the extra space after each identity was making it to not work
strange. and there was the error in line 63 also when you had things selected??? BTW you have spaces after the numbers: "10 ", "1 ", that might cause you another problem later, so fix it as well
|
0

It's throwing the notice and error because $_POST["authors"] isn't defined or there are no authors.

Simply use if(isset($_POST["authors"])) { to make sure data has been submitted.

Comments

0

change this:

foreach ($_POST["authors"] as $author) 
{

to this:

foreach ((array)$_POST["authors"] as $author) 
{

to make it empty-safe. Also i cannot see from where the "authors" are sent to addBook.php to make a suggestion..

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.