Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

update multiple tables

Status
Not open for further replies.

gadjodilo77

Programmer
Sep 23, 2005
79
NL
Hi,

I would like to now: is it possible to update 2 database tables with one form? How would this be done? (Inserting is fairly simple I believe, but updating....?)

Thank you in advance,

Yours, kabbi
 
expound a little? It can be as simple as writing two cfquerys if the data is not relational.

Does one table link to the other with a foriegn key that needs to have referencial inegrity?

if inserting is simple, then updating is too, I'm not sure exactly what part is holding you up, but more than willing to help.

 
Well It is a bit complicated. I have a database table(_devices) in which I inserted some data (devices with there descrioptions etc..).

Now I'm trying to make some sort of "assignmentform" like:

Get device nr: device_A (read from table: _devices)

Move device device_A from room: A
to
Room: B

description:


After I did this I want also put an actionstate for this device into the _devices table (actionstate: Move). The rest of the data, like the old and new location I want to save in another database table (_action).
I also want to put in some descriptions for that action, and the date when the action was started. This Information I also want to upload from the same form into the _action table

So now in the _devices table the device still is in room A but it has an action state called "move". The action being taken is stored into the _action table (room old and room new and date description etc). After the device is really moved from room A to room B, then the actionstate has to be put to normal state in the table _devices and the table _devices has to be updated with the new location! The data for that device entered in the _action table for that device has to be deleted at this moment.

Hopefully you understand what I mean! Can this be done at all?

The devices have serialnumbers which are unique (I made a serialnr field in both database tables so there can be some sort of relation. Both tables also have id nr's auto_increment primary key (but that are ofcourse different nr's).

What is also difficult is the fact that when there is no action in the _action table the data can be inserted, but what if an action is already in the _action table and there has to be made another action immediately.....
Best way I guess is:
When I make a new action, the current actionstate and actiondata in the table has to be deleted, and then the new state and data has to be inserted.

Hopefully you understand my explanation. It would be great if you could help me on my way.

(You also say without relational tables it would be simple, how would I do this then? Just make two separated queries?)

Thanks in advance,

Grt. kabbi
 
So the database"actions" after filling in the form would be:

1. update actionstate in table _devices

2. If for that device there already is an actionstate then-> the table _action have to be updated with the new data.
If there isn't already an actionstate (action data in the table _action) then -> the data has to be inserted in the table _action (including the device serialnr so that actionstate information can be displayed if neccessary for that device).

When the devices are actually moved:
use form to:

3. update _device table with the new data (room/location device, and no actionstate)
at same time:
4. Delete action data for that device out of the _action table. (because the device isn't in action anymore).

grts
 
What I have thusfar:

step 1.
step 2. Works if there is no action data in the _action table for that device.

the code:

<cfquery datasource="rug">
INSERT INTO actie (actie_id, actie_volgnr, actie_serienr, actie_actie, actie_opmerking, actie_medewerker, actie_datum, actie_van, actie_to, dev_id) VALUES (
<cfif IsDefined("FORM.actie_id") AND #FORM.actie_id# NEQ "">
#FORM.actie_id#
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.actie_volgnr") AND #FORM.actie_volgnr# NEQ "">
'#FORM.actie_volgnr#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.actie_serienr") AND #FORM.actie_serienr# NEQ "">
'#FORM.actie_serienr#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.actie_actie") AND #FORM.actie_actie# NEQ "">
'#FORM.actie_actie#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.actie_opmerking") AND #FORM.actie_opmerking# NEQ "">
'#FORM.actie_opmerking#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.actie_medewerker") AND #FORM.actie_medewerker# NEQ "">
'#FORM.actie_medewerker#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.actie_datum") AND #FORM.actie_datum# NEQ "">
'#FORM.actie_datum#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.actie_van") AND #FORM.actie_van# NEQ "">
'#FORM.actie_van#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.actie_to") AND #FORM.actie_to# NEQ "">
'#FORM.actie_to#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.dev_id") AND #FORM.dev_id# NEQ "">
'#FORM.dev_id#'
<cfelse>
NULL
</cfif>
)
</cfquery>
</cfif>

<cfquery name = "update" DATASOURCE = "rug">
UPDATE dev SET
dev_act= '#FORM.actie_actie#'
WHERE dev_id = #FORM.dev_id#
</cfquery>


 
Hi ,

to find our if your record already exists, check for a record in your 'actie' table, If one exists, run an update.

For the include example I have made the assumption that your 'actie_id' is the Primary Key of the 'actie' table.

Code:
<cfset dsn = "rug">          <!--- set the name of your datasource --->
<!--- define our db action --->
<cfset dbAction = "insert">
<!--- define our default form values --->
<cfparam name="form.actie_id"           default="0">
<cfparam name="form.actie_volgnr"       default="">
<cfparam name="form.actie_serienr"      default="">
<cfparam name="form.actie_actie"        default="">
<cfparam name="form.actie_medewerker"   default="">
<cfparam name="form.actie_datum"        default="">
<cfparam name="form.actie_van"          default="">
<cfparam name="form.actie_dev"          default="">


<!--- dupe check--->
<cfquery name="qDupes" datasource="#dsn#">
    SELECT actie_id 
    FROM actie
    WHERE actie_id = #form.actie_id#
</cfquery>

<cfif qDupes.recordcount>
    <cfset dbAction = "update">
<cfelse>
    <cfset form.actie_id = "">
    <cfset dbAction = "insert">
</cfif>
<!--- /dupe check --->
 
 
 <!--- db transaction --->
 <cftry>
     <cftransaction action="begin">
     
		 <cfswitch expression="#dbAction#">
			 <cfcase value="insert">
                <cfquery name="qAddRec" datasource="#dsn#">
                    INSERT INTO actie (actie_volgnr, actie_serienr, actie_actie, 
                    actie_opmerking, actie_medewerker, actie_datum, 
                    actie_van, actie_to, dev_id) 
                    VALUES (#dbValue(form.actie_volgnr)#, #dbValue(form.actie_serienr)#, #dbValue(form.actie_actie)#,
                    #dbValue(form.actie_opmerking)#, #dbValue(form.actie_medewerker)#, #dbValue(form.actie_datum)#,
                    #dbValue(form.actie_van)#, #dbValue(form.actie_to)#, #dbValue(form.actie_dev)#)
                </cfquery>
			 </cfcase>
			 
			 <cfcase value="update">
			      <cfquery name="qUpdateRec" datasource="#dsn#">
			      	UPDATE actie
			      	SET    actie_volgnr = #dbValue(form.actie_volgnr)#,
			      	       actie_serienr = #dbValue(form.actie_serienr)#,
			      	       actie_actie = #dbValue(form.actie_actie)#,
			      	       actie_opmerking = #dbValue(form.actie_opmerking)#,
			      	       actie_medewerker = #dbValue(form.actie_medewerker)#,
			      	       actie_datum = #dbValue(form.actie_datum)#,
			      	       actie_van = #dbValue(form.actie_van)#,
			      	       actie_to = #dbValue(form.actie_to)#,
			      	       dev_id = #dbValue(form.dev_id)#
			      	WHERE  (actie_id = #dbValue(form.actie_id)#)
			      </cfquery>
			 </cfcases>
		 </cfswitch>
     
		<cfquery name = "update" DATASOURCE = "rug">
		UPDATE dev SET
			   dev_act= #dbValue(form.actie_actie)#
		WHERE dev_id = #FORM.dev_id#
		</cfquery>     
     
     <!--- error handler --->
     <cfcatch type="any">
         <!--- undo any changes --->
         <cftransaction action="rollback">
         <!--- tell the user about it --->
         <span class="error"><strong>An error has occured</strong><br />
         We are sorry, something went wrong writing to the database.<br />
         <p><strong>Error detail:</strong><br /><cfoutput>#cfcatch.detail#</cfoutput><br /><br />
         <strong>Error message:</strong><br /><cfoutput>#cfcatch.message#</cfoutput></p></span>
     </cfcatch>
     <!--- /error handler --->
     </cftransaction>
 </cftry> 
 <!--- /db transaction --->
 
 <!--- udf --->
 <cfscript>
   function dbValue(sVariable) {
     if (IsDefined("#sVariable#")) {
         if (Trim(Evaluate(sVariable)) NEQ "") {
			sReturn = "'" & Trim(Evaluate(sVariable)) & "'";
         }
         else { sReturn = "NULL"; }
     }
     else { sReturn = "NULL"; }
     
     return sReturn;
   }
 </cfscript>
 <!--- /udf --->

Veel Geluk,
Bammy

We never fail, we just find that the path to succes is never quite what we thought...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top