I work for a large medical center, we submit a lot of grant applications to the federal government.
Each of these applications comes into my dept, where we process certain kinds of information. There are multiple personnel on each grant, each of whom need to submit certain paperwork to my office.
I have developed an administration tool for all of this. the application information is keyed and stored as a mySQL record. I want each person to be stored as a separate record in a separate table, but with an identifier to link each person record back to the grant application record.
This all needs to be done off of one submission form, rather than in multiple steps.
So right now, before my PHP script inserts the new grant record, it goes in and queries the index field and finds the record with the highest index #, then adds one. This is then an assumption of the value of the index field for the new grant record we're about to insert.
So then we insert the new grant record, and then we insert the persons (parsed from a text area field) each as a new person record, with a field for the grant record index field so you can tell what person goes with which grant (also because the same person can be on dozens of grants, so we may have dozens of person records with the same name).
The problem is this only works about 1/2 the time. I think what is happening is where I may have gaps in the grant index key values due to deleting records, etc, the calculated index key value isn't always the actual value once the grant record is inserted. I assumed auto_increment always added one to the highest existing value, but it doesn't appear to work that way. I need a way to index both tables that is infallible. Looking for other ideas.
here's my code:
// get number of rows in grants "Items" table
$queryROWS = "SELECT LogIndex FROM Items ORDER BY LogIndex DESC LIMIT 1";
$resultROWS = mysql_query($queryROWS) or die ("Error in queryROWS: $queryROWS. " . mysql_error());
$rowROWS = mysql_fetch_object($resultROWS);
$CurrentLogIndex = $rowROWS->LogIndex;
$LogIndex = $CurrentLogIndex+1;
// add new grant record to 'items'
$query = "INSERT INTO Items (PI, Unit, Sponsor, Title, Work_Type, CurrentStatus, NAD, Next_Action, Due_Date, Due_GCM, GA, Comments, Prop_Num, Opp_Num, Entry_Date, LogIndex) VALUES ('$PI', '$Unit', '$Sponsor', '$Title', '$Work_Type', '$CurrentStatus','$NAD_Date', '$Next_Action', '$Due_Date', '$Due_GCM', '$GA', '$AsComments', '$Prop_Num', '$Opp_Num', NOW(), '')";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
// add additional personnel as new records to 'add_persons'
// set counter to zero
$i = 0;
// split the phrase by any number of commas or space characters,
// which include " ", \r, \t, \n and \f
$keys = preg_split("/[\s,]+/", "$Personnel");
$num = count($keys);
while($i < $num) {
$name = $keys[$i];
$queryPERS = "INSERT INTO add_persons (PerIndex, person_id, LogIndex) VALUES ('', '$name', '$LogIndex')";
mysql_query($queryPERS) or die ("Error in queryPERS: $queryPERS. " . mysql_error());
$i++;
}