Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 10th, 2005, 12:53 AM
Registered User
Join Date: Apr 2005
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do I INSERT INTO multiple tables using array?

I am still a beginner at PHP and MySQL and have no clue how to do this. Your help is greatly appreciated! At least I had enough sense to know that I can achieve my goal by using an array, but the examples I have seen are too unrelated and difficult "for me" to transfer to what I am doing. I have learned everything I know from these forums and PHP & MySQL for Dummies!

For this learning experience, let's just say that I have an HTML form that contains 6 checkboxes on an HTML page called insert_array.html. The checkboxes are arranged in sets of 2 (e.g. 2 of the checkboxes have checkbox name="test1[]", 2 of the checkboxes have checkbox name="test2[]", and 2 of the checkboxes have checkbox name="test3[]"). Notice that I am using brackets "[]" to put the values in an array. I want to insert the 2 values for each set of checkboxes into three separate fields (e.g. field1, field2, field3) in 3 separate tables (e.g. tblTest1, tblTest2, tblTest3).

For example,

test1[] "Red Widget" and test1[] "Green Widget" should be inserted into tblTest1

test2[] "Blue Widget" and test2[] "Orange Widget" should be inserted into tblTest2

test3[] "Purple Widget" and test3[] "Yellow Widget" should be inserted into tblTest3

I want to POST the values of the checkboxes to a PHP page called insert_data.php. My first problem is that I do not know how to insert the values collected from the form into 3 separate tables. My second problem is that I do not know how to properly format the syntax to make the array work the way it is supposed to. Can somebody help me out on this one?

/* Here's the HTML form code for insert_array.html */

<form action="insert_data.php" method="POST">
<input type="checkbox" name="test1[]" value="Red Widget"><BR>
<input type="checkbox" name="test1[]" value="Green Widget"><BR>
<input type="checkbox" name="test2[]" value="Blue Widget"><BR>
<input type="checkbox" name="test2[]" value="Orange Widget"><BR>
<input type="checkbox" name="test3[]" value="Purple Widget"><BR>
<input type="checkbox" name="test3[]" value="Yellow Widget"><BR>
<input type="submit" name="submit" value="Insert Data">

/* Now here's the PHP code I have so far for insert_data.php */

/* NOTE: I do not have the code for the query(ies) needed to insert the data into all three tables because I do not know how to do it. Also, I do not know how to do an implode. I have read about this, but I am completely lost! I want to be able to read the checkbox values in the database and I have seen where using serialize (saw example using only one table) doesn't make this possible. I have tried using unserialize, but couldn't figure it out with the examples given. */

   $connection = mysql_connect($host,$user,$password)
     or die ("Couldn't connect to server");
   $db = mysql_select_db($database,$connection)
     or die ("Couldn't select database");

$test1 = stripslashes($_POST['test1']);
$query = INSERT INTO tblTest1 (field1) VALUES ('$test1');
$result = mysql_query($query) or die ("Could Not Insert Record Into Database.");

header("Location: http://www.some-web-site.com/some_page.php");


/* In the header, I have used a bogus URL. However, I would like to see the results of what I submitted on a separate Web page (e.g. some_page.php) */

The results should display something like this:

<H1>Here is what you submitted to the database...</H1>

<p align='left'>
    <LI>Red Widget
    <LI>Green Widget
    <LI>Blue Widget
    <LI>Orange Widget
    <LI>Purple Widget
    <LI>Yellow Widget

Thanks guys and gals!!!

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Command to insert data in multiple tables? dstein4d Access VBA 3 November 18th, 2008 09:16 AM
BULK INSERT FOR ARRAY MathLearner Beginning VB 6 4 September 28th, 2007 07:18 AM
Insert into Multiple Tables bmalex1 Beginning PHP 0 February 6th, 2006 12:45 PM
Importing Multiple files in Multiple tables Versi Suomi Access 6 June 1st, 2005 08:47 AM
Multiple Recordsets from Multiple Tables TSEROOGY Classic ASP Databases 2 December 28th, 2004 12:45 PM

All times are GMT -4. The time now is 07:00 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.