I'm building a fairly simple site to keep track of of some sales for work. It involves a mysql database with multiple tables for each entry. For the most part, the relationships are cut and dry. However, I have a comments table that will store multiple comments for the same sale. I have a foreign key in the comments table tied to the ID of the main sales table. I have a similar arrangement between a gross table and the sales table, in that it stores multiple gross amounts for the same saleID. What is the best way to insert these into the database? Currently, I'm inserting the comments and getting the ID of that row, then inserting the gross and getting the ID, then I make the insertion into the sales table using the comments id and the gross id. Is there a more efficient method rather than making 5 queries?
--Edit: Here is the current code. I'm pretty new to this so I had to look up procedures to see what you meant. It seems like it's basically what I'm doing, but creating a method for it instead, which I'll be doing once I figure out the most efficient way.
//Prepare the Queries
//Insert Gross Amounts Query
$grossQ = "INSERT INTO gross (commGross, storeGross, manGross, fiGross, flatGross) VALUES ('$commGross', '$storeGross', '$manGross', '$fiGross')";
//Insert Comments Query
$commentsQ = "INSERT INTO comments (comment, dealcomment, grosscomment) VALUES ('$otherComments', '$dealComments', $grossComments')";
//Insert Deal Query
$q = "INSERT INTO sales (make, model, location, vehtype, saletype, manager, county, flat, unitcount, armoramount, controlnumber, salesman, stock) VALUES ('$make', '$model', '$location', '$vehType', '$saleType', '$managerName', '$county', '$flat', '$unitCount', '$armorAmount', '$controlNumber', '$salesmanName1', '$stock')";
//Make database connection
$db = new db('palm_sales');
//Execute gross query and get the id of the row
$db->execute($grossQ);
$grossID = $db->getLastID();
//Execute comments query and get the id of the row
$db->execute($commentsQ);
$commentsID = $db->getLastID();
//Build Main Query
$q = "INSERT INTO sales (make, model, location, vehtype, saletype, manager, gross, comments, county, flat, unitcount, armoramount, controlnumber, salesman, stock) VALUES ('$make', '$model', '$location', '$vehType', '$saleType', '$managerName', '$grossID', '$commentsID', '$county', '$flat', '$unitCount', '$armorAmount', '$controlNumber', '$salesmanName1', '$stock')";
//Execute Main Query
$db->execute($q);
if(mysql_affected_rows() > 0)
{
echo "Success!";
}
else echo "Error: ".mysql_error();