lundi 23 mars 2015

Selecting multiple fields from and adding them to database

Background:


I am creating a three tier e commerce website which sells books. Within my website I have created a form (only accessible to staff members) which allows the staff member to add a new book to the system (the database).


At the moment I have a table within my database which records the following data:



book_isdn // unique identifier of each book.
book_cat
book_title
book_author
etc..


Along with this, I have created a table for book categories which stores the following:



cat_id
cat_title


I have defined the following rows in the categories table:



cat_id 1 = Business books
cat_id 2 = Computing books
cat_id 3 = Science books
cat_id 4 = History books
etc


The problem:


In the form which allows a staff member to add a new book, I have a list:



<select multiple name="b_category" style = "width:150px" required>
<?php
$get_cats = "select * from categories";
$run_cats = mysqli_query($connect, $get_cats);

while ($row_cats = mysqli_fetch_array($run_cats)) {

$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_title'];

echo "<option value='$cat_id'> $cat_title </option>";
}
?>
</select>


I want to add a new book to the 'books' table with the corresponding cat_id for the category to which the book belongs to (i.e. business, computing etc.).


However, a book can also be in two categories, i.e. a book can be both in the field of business and computing.


The question:


How can I alter the form so that it selects multiple options from and adds them to the database, along with the cat_id?


For example:


if using the form I complete all other fields and select computing and business from the list, I want it so that upon clicking "Add new book", the form data is sent to the 'books' table where I will be able to see the new book and under the field of book_cat, I will see 1,2.


I am completely stumped. Is there any way to approach this issue? I hope I have explained this well.


Thanks.


Aucun commentaire:

Enregistrer un commentaire