Topic awaiting preservation: How to create MySQL rows using individual array entry items... |
|
---|---|
Author | Thread |
Nervous Wreck (II) Inmate From: |
posted 08-15-2005 00:21
I'm building a form and using PHP to create text fields based on a user selection. I want the individual to select the number of children in their family from a dropdown, then return the page & auto-create "Child x Name" and "Child x Age" fields per the number of children they selected. Here's the portion of code: code: <form name="children_number" method="post" action="<?php echo($_SERVER['PHP_SELF']); ?>?children=self"> ... Using javascript to submit the form when the number of children is selected from the dropdown menu. New form uses this number to auto-create the fields as follows: // Create child fields from number of children selected on dropdown menu if (isset($children)) { $number = $_POST['children_number']; for ($j=1; $j<$number+1; $j++) { echo ("\t\t\t\t<tr>\n"); echo ("\t\t\t\t\t<td> Child " . $j . " Name</td>\n"); echo ("\t\t\t\t\t<td width=\"220\"><input type=\"text\" name=\"child_name[]\" size=\"30\"></td>\n"); echo ("\t\t\t\t</tr>\n"); echo ("\t\t\t\t<tr>\n"); echo ("\t\t\t\t\t<td> Child " . $j . " Age</td>\n"); echo ("\t\t\t\t\t<td width=\"220\"><input type=\"text\" name=\"child_age[]\" size=\"30\"></td>\n"); echo ("\t\t\t\t</tr>\n"); } }
|
Bipolar (III) Inmate From: f(x) |
posted 08-15-2005 01:13
Something like this? code: // Connection to database is allready open. foreach ($child_name AS $key=>$val){ $query = "INSERT INTO `child` (`parent_id`,`child_name`,`child_age`)" ." VALUES ('$parent','$child_name[$key]','$child_age[$key]')"; if (mysql_query($query)) echo "Child $key was added to the database!"; else echo "Child $key was not added to the database! MySQL said: ".mysql_error(); }
|
Nervous Wreck (II) Inmate From: |
posted 08-15-2005 01:51
quote:
|
Bipolar (III) Inmate From: f(x) |
posted 08-15-2005 02:36
Well, you have two database tables, let's assume you have the database 'kristo_people' and the tables 'parents' and 'children'. You can associate them with an id, the parent id. code: // The tables would be like: CREATE TABLE `parents` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT , `mr_name` VARCHAR( 30 ) NOT NULL , `mrs_name` VARCHAR( 30 ) NOT NULL , `last_name` VARCHAR( 50 ) NOT NULL , `description` TEXT NOT NULL , `joined` TIMESTAMP NOT NULL , PRIMARY KEY ( `id` ) ); CREATE TABLE `children` ( `parent_id` INT UNSIGNED NOT NULL , `name` VARCHAR( 30 ) NOT NULL , `age` TINYINT( 2 ) UNSIGNED NOT NULL );
code: INSERT INTO `parents` ( `id` , `mr_name` , `mrs_name` , `last_name` , `description` , `joined` ) VALUES ( '', 'Joe', 'Sarah', 'Smith', 'They grew up in a small town.', NOW( ) ), ( '', 'David', 'Donna', 'Brown', 'They grew up in a large city.', NOW( ) ); INSERT INTO `children` ( `parent_id` , `name` , `age` ) VALUES ( '1', 'Ken', '11' ), ( '1', 'Tyler', '14' ), ( '2', 'Billy', '10' ), ( '2', 'Jimmy', '12' ), ( '2', 'Tommy', '23' );
code: // Allready connected to database... $query1 = "SELECT * FROM `parents`"; $result1 = mysql_query($query1); while ($row1 = mysql_fetch_array($result1,MYSQL_ASSOC)){ $children = array(); $query2 = "SELECT * FROM `children` WHERE `parent_id`='$row1[id]' ORDER BY `name` ASC"; $result2 = mysql_query($query2); while ($row2 = mysql_fetch_array($result2,MYSQL_ASSOC)){ $children[] = "$row2[name] ($row2[age])"; } echo "<p>$row1[mr_name] & $row1[mrs_name] $row1[last_name] have the children " .implode(", ",$children).".<br />\n$row1[description]</p>\n"; }
code: <p>Joe & Sarah Smith have the children Ken (11), Tyler (14).<br /> They grew up in a small town.</p> <p>David & Donna Brown have the children Billy (11), Jimmy (14), Tommy (23).<br /> They grew up in a large city.</p> |
Nervous Wreck (II) Inmate From: |
posted 08-15-2005 04:11
Yeah, that explains it really well. I tried implementing it, but am getting the following error message: quote:
code: $doc_root = $_SERVER['DOCUMENT_ROOT']; // Start session session_start; // Connect to the database include ("$doc_root/includes/connect.php"); // Define variables $primary_adult_name = $_SESSION['primary_adult_name']; $secondary_adult_yn = $_SESSION['secondary_adult_yn']; $secondary_adult_name = $_SESSION['secondary_adult_name']; $children_number = $_SESSION['children_number']; $child_name = $_SESSION['child_name']; $child_age = $_SESSION['child_age']; $receive_reminder_yn = $_SESSION['receive_reminder_yn']; $reminder_frequency = $_SESSION['reminder_frequency']; $username = $_SESSION['username']; $password = $_SESSION['password']; $email = $_SESSION['email']; $church_code = $_SESSION['church_code']; // Add family to database $sql = "INSERT INTO heart_family VALUES ('', '$primary_adult_name', '$secondary_adult_yn', '$secondary_adult_name', '$children_number', '', '$church_code', '$receive_reminder_yn', '$reminder_frequency')"; $result = mysql_query($sql) or die ("<p class=blue>Error adding family: mysql_error() .</p>"); // Get family_id from new record $sql = "SELECT * FROM heart_family WHERE primary_adult_name = '$primary_adult_name'"; $result = mysql_query($sql) or die("Couldn't retrieve family."); while($row = mysql_fetch_array($result)) { $family_id = $row['family_id']; $_SESSION['family_id']=$family_id; } // Add children to database foreach ($child_name AS $key=>$val) { $query = "INSERT INTO heart_child (child_id, child_name, child_age, family_id) VALUES ('', '$child_name[$key]','$child_age[$key]', '$family_id')"; if (mysql_query($query)) echo "Child $key was added to the database!"; else echo "Child $key was not added to the database! MySQL said: ".mysql_error(); }
|
Nervous Wreck (II) Inmate From: |
posted 08-15-2005 04:30
It worked! code: // Start session session_start;
code: // Start session session_start();
|
Bipolar (III) Inmate From: f(x) |
posted 08-15-2005 17:48 |