Forum Moderators: coopster
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.
<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</font></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</font></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</font></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>';
?>
then all you really need to do is imitate the various steps you did in 2 steps but do them in the processing script at the same time.
You create them in the order they need to be created, get the ids of those things then use those for your next steps.
I agree with you, that's the plan, but the approach is faulty. If I wanted to make it a two-step process, I would be GETting the ID such as:
$family_id = $_GET['family_id'];
$query = "SELECT * FROM family WHERE family_id = '$family_id'";
$result = mssql_query($query);
$row = mssql_fetch_array($result);
but when I try to do it in one step, I am unsure how to tie the family_id to the childset_id, as the family_id is unknown to me until the form is submitted(I think?!)
Is there a way to tell what the ID will be and tie the childset id to it?
mysql_insert_id [php.net]
afterwards to get the id from the new family
I have looked at websites which compare the differences between the MSSQL and MySQL stuff, but I did not see one for the mysql_insert_id
[edited by: BadGoat at 8:17 pm (utc) on May 8, 2006]
jatar, is it possible to insert it as recommended and still keep the process down to one step?
for the user, yes it can be one step
the processing involved is exactly the same either way.
The big difference is that the information you pre select in the two step method is not available in a single step (for the user) method so the processing script will have to figure that stuff out.
just as a hint you should really have your dev and production setup the same way, will save you a lot of hair loss ;)
<added>I see you are referring to seperate projects, sry
you could use an abstraction layer for your db so that it doesn't matter as much which db you are using and that would allow for better code portability.
$father_id =$_POST['father_id'];
$mother_id =$_POST['mother_id'];
$childset_id =$_POST['childset_id'];
$sqlquery = "INSERT INTO family VALUES('','". $_POST['father_id'] ."','". $_POST['mother_id'] ."','". $_POST['childset_id'] ."')";
$queryresult = mysql_query($sqlquery) or die(" Could not execute mysql query!");
$sqlquery = "SELECT * from family";
My confusion is understanding how to superficially make it a one-step process for the user. Is there a tutorial which I could glean some insight from?
(to better explain: I do projects at work which I must use MSSQL and also do projects at home where use MySQL, and use the knowledge gained on either project to assist in the completetion of the other.)