View Single Post

  #2 (permalink)  
Old 12-19-2004
Pugi!
 
Posts: n/a
Default Need help with PHP-MYSQL-Forms and checkboxes (lots of them)

I designed a book database (mysql) and am now making the user interface for
it (php).
The main table is books and contains quite a lot of information (title,
subtitle, isbn, pages, size, ...) but for example not the author, because a
book can have more then one author. So I also have a table authors and a
table link_books_authors.
The same with genres (humour, action, fantasy, history, spy, ....); a book
can belong to more then one genre.
The list of genres is represented in the input form by a list of checkboxes


<table>
<tr>
<td><input type="checkbox" name="5" value="1"> Action</td>
<td><input type="checkbox" name="4" value="1"> Biology</td>
<td><input type="checkbox" name="8" value="1"> Chemistry</td>
<td><input type="checkbox" name="3" value="1"> Fantasy</td>
</tr>
<tr>
<td><input type="checkbox" name="15" value="1"> Horror</td>
<td><input type="checkbox" name="7" value="1"> Science</td>
<td><input type="checkbox" name="2" value="1"> Science-Fiction</td>
<td><input type="checkbox" name="9" value="1"> Tao</td>
</tr>
<tr>
...
</tr>
</table>


The list is automatically created, alphabeticaly, with the contents of the
table genres and the "name" is the id of the genre in the genres-table.
So far so good. Now I must proces the information of the form and insert it
in the database. If I hardcode the processing of each genre, still no
problem :

if (isset($_REQUEST["1"]) && ($_REQUEST["1"] == 1)
{
$sqlBookGenre = "INSERT INTO link_books_genres VALUES (" . $intBookID
.. ", " . $_REQUEST["1"] . ")";
}

execute query and then next checkbox

if (isset($_REQUEST["2"]) && ($_REQUEST["2"] == 1)
{
$sqlBookGenre = "INSERT INTO link_books_genres VALUES (" . $intBookID
.. ", " . $_REQUEST["2"] . ")";
}

For the moment I have already 15 genres, but I will create more, so I have
to put the above coding in a loop. So again I read the list of genres (at
least the id's) and store them in an array $arrGenres.
With a while-loop I want to check if a checkbox is checked (isset and value
== 1) or not.
But how do I do that. How do a obtain the value of $_REQUEST["1"],
$_REQUEST["2"], ... when it is in a loop ?
$_REQUEST[$arrGenres["id"]] and $_REQUEST["$arrGenres["id"]"] don't work. I
also tried to put the values of the checkboxes on the screen with echo and
printf, but didn't succeed in it.
I suppose an alternative would be a select or listbox and allowing multiple
selects but I don't have an example for that either.

Thanx for ur time.

Pugi!

Reply to group


Reply With Quote