Forum Moderators: open

Message Too Old, No Replies

How to insert data into multiple tables using one HTML form

..using various form elements

         

pulszar

4:01 pm on Nov 14, 2006 (gmt 0)

10+ Year Member



I found a few threads here and on the web that come a little bit close to this, but not enough to where I was able to figure it out:

Using PHP and MySQL, say I have an html form with a couple single line text fields that enter data into a table (date, title, link, story). Then say I have a series of check boxes that are labled with different categories: (news, reviews, articles, products, etc). They associate with another table.

I have 3 tables in my db: stories, categories, story_category

stories table has: (story_id, date, title, link, etc)
categories table has: (category_id, name)
story_category table has: (story_category_id, story_id, category_id)

Even though I'm not 100% yet on making this work, this thread about categories was very helpful: [webmasterworld.com...]

I'll figure that part out, but what I'm lost on is how to use one html form with different elements (text field, checkbox) to insert data into 2 different tables. I know its all in the query statement, but since this method of categories uses the story_id multiple times in the story_category table if there is more than one category, incorporating this with a check box is throwing me off.

I know how to make a form and have the data inserted into a single table in a database. Its the checkboxes and second table that are throwing me off. Any suggestions are very much appreciated. Thanks.

volatilegx

10:18 pm on Nov 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're going to want to use multiple queries and after the inserts to the stories table and categories table use the mysql_insert_id [us2.php.net] function (I assume you are working in PHP) to get the story_id and category_id for your insert query for the story_category table.

mcibor

10:15 pm on Nov 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As I recall, the checkboxes return value "on", when they are checked and empty when left unchecked.

so to check against a checkbox:
if(isset($_POST['chbox']) && $_POST['chbox'] == "on") $chbox = 1;
else $chbox = 0;

Sorry for putting php into mysql forum ;)

And mysql_insert_id will do just fine.

Regards
Michal