Hello,
I have been trying to write a script which will allow me to input family member names and pair them up to make families. It was easy enough for me to make scripts to create father, mother, and children names, but I am having issues when trying to bring it all together to create the family.
In my code below, I have dropdown menus which allow me to select the father, mother, and child. But I cannot figure out how to add multiple children before submitting the family. I should say that I did figure out how to do this if I submitted the mother and father first to create the family and then add children as a second step, but I want to be able to add all the children in one step.
I thought the best way would be to add the children by child_id to a childset table and reference the family_id to the childset_id, but since the family hasn't been created yet, I couldn't think of how to get the id that would be used once submitted. I also thought that it would be ideal if I could select the child's name from the dropdown and then have it echoed on the same page so I would know who I have already added for that family.
Please pardon my code, I'm sure it's far from efficient and far from correct.
Any guidance is most appreciated!
<html>
<head>
<title>Add Family</title>
</head>
<body>
<?PHP
echo '
<table>
<form name="form1" method="post" action="family_added.php">
<tr>
<td align="center" colspan="2"><b>Add Family</b></td>
</tr>
<tr>
<td><B>Father</B></td>
<td><select name="father_id" tabindex="4"><option value="notset">- Father -</option> ';
//--- CREATE FATHER SELECT ---
$sql = "SELECT DISTINCT father_id, father_name FROM father ORDER BY father_name";
$dad = mysql_query($sql) or die($sql . '<br />' . mysql_error());
while ($row = mysql_fetch_array($dad)) {
echo '<option value="' . $row['father_id'] . '">' . $row['father_name'] . '</option>';
} ;
echo '</select></td>
</tr>
<tr>
<td><B>Mother</B></td>
<td><select name="mother_id" tabindex="4"><option value="notset">- Mother -</option> ';
//--- CREATE MOTHER SELECT ---
$sql = "SELECT DISTINCT mother_id, mother_name FROM mother ORDER BY mother_name";
$mom = mysql_query($sql) or die($sql . '<br />' . mysql_error());
while ($row = mysql_fetch_array($mom)) {
echo '<option value="' . $row['mother_id'] . '">' . $row['mother_name'] . '</option>';
} ;
echo '</select></td>
</tr>
<tr>
<td><B>Children</B></td>
<td><select name="child_id" tabindex="4"><option value="">- Child -</option> ';
//--- CREATE CHILD SELECT ---
$sql = "SELECT DISTINCT child_id, child_name FROM child ORDER BY child_name";
$child = mysql_query($sql) or die($sql . '<br />' . mysql_error());
while ($row = mysql_fetch_array($child)) {
echo '<option value="' . $row['child_id'] . '">' . $row['child_name'] . '</option>';
} ;
echo '</select></td>
</tr> ';
?>
<?
$sqlquery = "SELECT child.child_id, child.child_name, childset.child_id FROM child, childset WHERE child_id = childset.child_id ORDER BY child_name";
$queryresult = mysql_query($sqlquery) or die(" Could not execute mysql query !");
$row = mysql_fetch_row($queryresult);
$child_name = $row[0];
$tdcount = 1;
$numtd = 1;
echo'
<tr>
<td>Children</td>';
mysql_data_seek($queryresult,0) ;
while($row = mysql_fetch_array($queryresult)) {
if ($tdcount == 1) echo "<tr>";
echo '
<td>'.$row['child_name'].'</td>' ;
if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;
}
}
// time to close up our table
if ($tdcount!= 1) {
while ($tdcount <= $numtd) {
echo "<td> </td>";
$tdcount++;
}
echo "</tr>";
}
echo '
<tr>
<td colspan=2><center><br><input type="submit" name="Submit" value="Submit" action="required"></center></td>
</tr></form>';
?>