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!!!
|