how to insert multiple rows from MySQL with checkbox?

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

  • 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. <input type="checkbox" name="room_type_id[]" value="1" /> Standard Twin</label> <input type="checkbox" name="room_type_id[]" value="2" /> Standard Queen</label>\ <input type="checkbox" name="room_type_id[]" value="3" /> Family</label> 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

  • Answer:

    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.

thomasal... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

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.

AnalProgrammer

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.