Home » questions » how to insert a multiple value into database but not a single column?

how to insert a multiple value into database but not a single column?

2006-08-15 02:11:05, Category: Programming & Design
I have a table in my database (call roomtype) with the rows roomtype_id(Primary Key) and room_type. I have another table (call bookdetail) with the rows book_id, guest_ic and roomtype_id.I have a simple form that allow user check the checkbox which room they want to stay. Standard Twin Standard Queen\ Family after they check, will post it to another page.Did anybody know how to insert the value to the field roomtype_id in table bookdetail?Eg: book_id -----guest_ic--------------- roomtype_id 2 --------840116 -------------- 1 2 ---------840116 ------------- 1 2 ------------840116 -------------- 2 but my problem is the roomtype_id only store a last value:2 but the value 1 never store?Can anybody help?Pls if(!isset($_POST['room_type_id'])) { //array is empty $_POST['room_type_id'] = array(); echo "None selected."; } else { //must do this for checkboxes (passing multiple values) foreach ($_POST['room_type_id'] as $items) { echo $items; } } when i echo to see the result, i get :112 and then value in field roomtype_id (table bookdetail) will only store the value "2".Where is another two value "1" store?Can anybody help me solve this problem?I'm new in php.Pls help.... i have try to search this problem since last week, but untill now i cant find a solution.Pls help.... thank you :(

Answers

  1. AnalProgrammer

    On 2006-08-15 06:08:29


    Your check boxes need to be given a unique name like room_type_id_1 and room_type_id_2 etc. That should fix it.
  2. thomasalanshearer

    On 2006-08-15 02:21:34


    Why don't you use a radio button instead of a checkbox? I presuem they can only stay in one type of room? If not.. and you do want to store multiple values u just need to interate through the room_type_id array which will be stored in the page you are posting the data to.. If you are using php.. print_r(room_type_id); this will print out your array so you can check that you have the correct values.. then for each value.. write a row to the database.. use the foreach php function.. check out the source below. . I think this should solve your problem.