kenwvs Posted September 7, 2006 Share Posted September 7, 2006 I have a designed a form that people will fill out to generate a work order for a piece of equipment when it fails.I have created relational (I think) databases, where the tables are all normalized, but am not sure how to tie it all together now.As an example I have the following tables, with their purpose:workorder - majority of the data goes in this tableparts - the parts number and parts descriptions go in this tableemployees - this table contains the employees data and the employee who generated the workorder go in hereworkorder_parts - to tie the workorder and parts tables togetherworkorder_employee - to tie the workorder and employee tables togetherMy host does not offer innodb table type so I need to do this manuallyWhen I create the query to INSERT the data to the DB table, am I using INSERT INTO [color=red]TABLE[/color] where [color=red]TABLE is the workorder, parts or employees tables, or is it the workorder_parts, etc. table.[/color]I am thinking it is the main (parent) tables, but I don't understand how I will tie them together. As an example, if I use the parts table, how do I tie it together so that the parts number/description in the parts table are referenced to the workorder number they were generated from?Here is the code I am using:[code]if (!isset($_POST['Submit'])) {else{ $work = $_POST["work"]; $sched = $_POST["sched"]; $name = $_POST["tech"]; $site = $_POST["site"]; $serial = $_POST["serial"]; $hours = $_POST["hours"]; $starts = $_POST["starts"]; $issue = $_POST["issue"]; $severity = $_POST["severe"]; $resolution = $_POST["resolve"]; $assistance = $_POST["assist"]; $safety = $_POST["safe"]; $number = $_POST["number"]; $description = $_POST["description"];{ if (!empty($_POST['number'])) foreach($_POST['description'] as $key=>$description) { $description_esc = mysql_real_escape_string($description); $number = intval($_POST['number'][$key]); $query = "INSERT INTO parts (description,number) VALUES (" . "'{$description_esc}', " . "{$number} )"; mysql_query($query) or die(mysql_error().$query);} mysql_query ("Insert into `workorder`(work, sched, site, serial, hours, starts, issue, severity, resolution, assistance, safety) VALUES ('$work', '$sched', '$site', '$serial', '$hours', '$starts', '$issue', '$severity', '$resolution', '$assistance', '$safety')")or die(mysql_error()); mysql_query ("Insert into `employees` (name) VALUES ('$name')")or die(mysql_error());[/code] Quote Link to comment Share on other sites More sharing options...
drranch Posted September 7, 2006 Share Posted September 7, 2006 you would setup the following in your database...One to many relationship - Employee would be one and the workorder would be many.So each employe can have many workorder records associated with them.Table structuremake sure that your workorder table has a field for the employee name and set it as the 'key' for that table. This will prevent more than one employee getting added to this table. I don't think you want to do the one to many relationships with the parts/workorder section as many parts can be associated with many workorders and vice versa. What program are you using to upload your tables with? I use Plesk and the 'key' terminology is 'unique'. Quote Link to comment Share on other sites More sharing options...
fenway Posted September 7, 2006 Share Posted September 7, 2006 As for relating the records, simply pull back LAST_INSERT_ID(). Quote Link to comment Share on other sites More sharing options...
obsidian Posted September 7, 2006 Share Posted September 7, 2006 [quote author=fenway link=topic=107201.msg429836#msg429836 date=1157633058]As for relating the records, simply pull back LAST_INSERT_ID().[/quote]or, you could use the PHP function mysql_insert_id(). same result Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.