Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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">
</form>


/* 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. */

<?php
   include("connectme.php");
   $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'>
  <UL>
    <LI>Red Widget
    <LI>Green Widget
    <LI>Blue Widget
    <LI>Orange Widget
    <LI>Purple Widget
    <LI>Yellow Widget
  </UL>
</p>

Thanks guys and gals!!!






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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.