473,396 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to use a form to insert OR update

Hi all,

I am nearing the end of programming my select forms and I will be soon need to create my insert and update forms. I have a question as to how this is best handled.

I want to use one form to do the inserts and updates. Now, as far as the data in the database goes, if I have some data in a table already and only 1 field needs to be updated how does this work? Should I use an "if" conditional and isset on every field in the form or just update the whole table?

In other words, if the user wants to update a record and only changes 1 field in the form, I would *think* that only that 1 field should be updated and not all of the fields in the form. Am I correct on this?

I am also curious as how to handle the INSERT of new data as well. Lets say that the same form from above has 3 fields that have *NO* data and the user wants to update 1 or 2 fields in the form and sees that 3 of these fields are blank and then decides to fill it in. Those fields that the user just filled in will need an INSERT and not an update. How is this best done?

I suppose I am asking exactly how simultaneous updates and inserts are handled.

Thanks,

Frank
Sep 7 '07 #1
14 3156
Atli
5,058 Expert 4TB
Hi Frank. Welcome to The Scripts!

It is very important to always validate any data that you receive from your form. Especially if you are going to be inserting it into a Database.
If you don't, a hacker might be able to clean out your entire database by typing SQL syntax into the form input fields!
Check out the htmlentities() function, if you need a place to start.

As for the update queries. I agree with you that you should not update fields that have not been changed. It is, however, often more trouble that it is worth, trying to find out if a field has been changed or not. I would not go through any real trouble to avoid updating data that has not changed, but I would prefer it if possible (especially if they contain large amounts of data).

Even if a field is empty in a row that already exists it will technically have a value, even if it is NULL (tho in reality NULL is not a value). So to insert data into such a field you would use an UPDATE command.
INSERT is only used to create new rows.
Sep 7 '07 #2
Hi Frank. Welcome to The Scripts!

It is very important to always validate any data that you receive from your form. Especially if you are going to be inserting it into a Database.
If you don't, a hacker might be able to clean out your entire database by typing SQL syntax into the form input fields!
Check out the htmlentities() function, if you need a place to start.

As for the update queries. I agree with you that you should not update fields that have not been changed. It is, however, often more trouble that it is worth, trying to find out if a field has been changed or not. I would not go through any real trouble to avoid updating data that has not changed, but I would prefer it if possible (especially if they contain large amounts of data).

Even if a field is empty in a row that already exists it will technically have a value, even if it is NULL (tho in reality NULL is not a value). So to insert data into such a field you would use an UPDATE command.
INSERT is only used to create new rows.
Hi Atli,

A couple of things. Firstly, I noticed that somehow... I have two accounts on TSDN. I have an account called "fjm" as well. Is there any way to consolidate accounts or maybe just delete this one? :)

Your right. data validation is something that I will be using in the program and is really nessessary. You are talking about injection, I know..

So, if I am understanding you correctly Alti, you are saying to simply just update all info on the form, right? That will be *much* easier indeed.

Ok. So again, if I am understanding you correctly, INSERT is only to create a new row and UPDATE just updates the *existing* row even if there is no value in a given field. Correct?

Here would be my issue then. I do not want to have a seperate "New record" and "Edit" buttion. I would prefer to have just one button for BOTH inserts and updates and program some logic into the script to decide what needs to happen. Is this possible?

Thanks,

Frank
Sep 7 '07 #3
Atli
5,058 Expert 4TB
Hi Atli,

A couple of things. Firstly, I noticed that somehow... I have two accounts on TSDN. I have an account called "fjm" as well. Is there any way to consolidate accounts or maybe just delete this one? :)
I don't know about deleting it but it is probably possible to ban it :P
Tho it would probably be easier if you just stopped using it and logged into the other one.

So, if I am understanding you correctly Alti, you are saying to simply just update all info on the form, right? That will be *much* easier indeed.
Much easier indeed. But it would maybe be good idea to come back to that when you create the first Service Pack :)

Ok. So again, if I am understanding you correctly, INSERT is only to create a new row and UPDATE just updates the *existing* row even if there is no value in a given field. Correct?
Correct.
Here would be my issue then. I do not want to have a seperate "New record" and "Edit" buttion. I would prefer to have just one button for BOTH inserts and updates and program some logic into the script to decide what needs to happen. Is this possible?
Everything is possible! :)

As I imagine this scenario, you would have to choose one or more fields as Unique fields. Then when your user fills out the form you would have to check whether the Unique fields exist. If they do you would update them. If they don't you would create a new row.

I personally would create a 'New' button and an 'Edit' button, but that's just me.
Sep 7 '07 #4
As I imagine this scenario, you would have to choose one or more fields as Unique fields. Then when your user fills out the form you would have to check whether the Unique fields exist. If they do you would update them. If they don't you would create a new row.

I personally would create a 'New' button and an 'Edit' button, but that's just me.
Ok, I *may* take your advice on having two buttons. Well, come to think of it, I do have an initial "New" button for the insert of 1 form and an "Update" button for the updates on ALL forms. Follow me on this Atli and see if you can help me a little more please.

I have my data pretty well normalized, so lets take an example of having 4 tables having to do with a customer, ok? These 4 tables comprise information about the customer's address, company name, email, etc..

I was initially thinking to use the New button to insert only the first table of information on the first form. Now that you have opened my eyes to inserting a new row, couldn't I just INSERT into all of the tables having to do with the customer across the board then UPDATE as needed?

Thanks,

Frank
Sep 8 '07 #5
Atli
5,058 Expert 4TB
You could do that yes, but how well that will work out depends on you table structures.
It wouldn't make sense for me on my databases but it might for you. Could you post the table structures?
Sep 8 '07 #6
You could do that yes, but how well that will work out depends on you table structures.
It wouldn't make sense for me on my databases but it might for you. Could you post the table structures?
:) I would like to post the table structures but unfortunately there is a max upload limit on this forum. There are 114 tables total in the db. I actually came back to tell you that I do not think that will work simply because I am using Innodb (MySQL) and because of the constraints on the tables and not allowing null values. I'm stumped, whooped and out of answers.

:(
Sep 8 '07 #7
Atli
5,058 Expert 4TB
I see...

Well guess that means you will have to create some logic to find out whether a row exists or if you need to create it. Shouldn't be that hard.

Or if you want to bypass the whole InnoDB constraints not allowing Null values problem you can always invent some data into the fields until your customers update them :)
Sep 8 '07 #8
I see...

Well guess that means you will have to create some logic to find out whether a row exists or if you need to create it. Shouldn't be that hard.

Or if you want to bypass the whole InnoDB constraints not allowing Null values problem you can always invent some data into the fields until your customers update them :)
Hi Atli,

Thanks for the response. I was thinking about maybe putting default values and letting the user enter the data later, The only problem I see with that are duplicate rows and my constraints. There would only be one allowable row with those default values and a second row of data would fail becuase of the unique constraints.

Wouldn't I need to go through a bunch of selects first to find out what data if any has already been inserted into the db before determining whether to use an insert or update? That definetely sounds like A LOT more work then I had planned on. Or am I incorrect on this?
Sep 8 '07 #9
Atli
5,058 Expert 4TB
Wouldn't I need to go through a bunch of selects first to find out what data if any has already been inserted into the db before determining whether to use an insert or update? That definetely sounds like A LOT more work then I had planned on. Or am I incorrect on this?
Well, you would only have to query to see if the customer already has a row in the table. I am assuming each customer has an ID that is referenced in each of the other tables?
If so you could probably build a single query to check the all. Somewhat like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   t1.ID AS 't1',
  3.   t2.ID AS 't2',
  4.   t3.ID AS 't3'
  5. FROM customer
  6. LEFT JOIN t1
  7.   ON t1.cID = customer.ID
  8. LEFT JOIN t2
  9.   ON t2.cID = customer.ID
  10. LEFT JOIN t3
  11.   ON t3.cID = customer.ID
  12. WHERE customer.ID = 2
  13.  
Where t1, t2 and t3 are referencing the ID of the customer table as 'cID'.

If there are no rows in tables t1, t2 and t3 that are linked to the given customer.ID then their respective fields will return a NULL.

So if a table returns NULL you would have to user INSERT, else you would have to use UPDATE.
Sep 8 '07 #10
Well, you would only have to query to see if the customer already has a row in the table. I am assuming each customer has an ID that is referenced in each of the other tables?
If so you could probably build a single query to check the all. Somewhat like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   t1.ID AS 't1',
  3.   t2.ID AS 't2',
  4.   t3.ID AS 't3'
  5. FROM customer
  6. LEFT JOIN t1
  7.   ON t1.cID = customer.ID
  8. LEFT JOIN t2
  9.   ON t2.cID = customer.ID
  10. LEFT JOIN t3
  11.   ON t3.cID = customer.ID
  12. WHERE customer.ID = 2
  13.  
Where t1, t2 and t3 are referencing the ID of the customer table as 'cID'.

If there are no rows in tables t1, t2 and t3 that are linked to the given customer.ID then their respective fields will return a NULL.

So if a table returns NULL you would have to user INSERT, else you would have to use UPDATE.
Atli,

Thanks for that clarification. You are correct by the way; each table has a foreign key from the customer table. So after the joins, if I do this:

[php]
if(isset($myRow)){
mysql_query( "UPDATE bla bla" );
}else{
INSERT blabla
}
[/php]

I think that would work out nicely. :)
Sep 8 '07 #11
Atli
5,058 Expert 4TB
Atli,

Thanks for that clarification. You are correct by the way; each table has a foreign key from the customer table. So after the joins, if I do this:

[php]
if(isset($myRow)){
mysql_query( "UPDATE bla bla" );
}else{
INSERT blabla
}
[/php]

I think that would work out nicely. :)
Yes, thats basically what I was thinking.

Except you would have to do more than just check the row. You would have to check the data in the row to see if each of the table needs to be UPDATED or INSERTED. Somewhat like this:
Expand|Select|Wrap|Line Numbers
  1. $row = mysql_fetch_assoc($result);
  2. foreach($row as $tblName => $tblHasRow) {
  3.   if($tblHasRow != null) {
  4.     # Update
  5.   }
  6.   else {
  7.     # Insert
  8.   }
  9. }
  10.  
Sep 8 '07 #12
Yes, thats basically what I was thinking.

Except you would have to do more than just check the row. You would have to check the data in the row to see if each of the table needs to be UPDATED or INSERTED. Somewhat like this:
Expand|Select|Wrap|Line Numbers
  1. $row = mysql_fetch_assoc($result);
  2. foreach($row as $tblName => $tblHasRow) {
  3.   if($tblHasRow != null) {
  4.     # Update
  5.   }
  6.   else {
  7.     # Insert
  8.   }
  9. }
  10.  
Ah yes, I left our the fetch_assoc()

Ok, that sounds very doable indeed. Atli, thank you VERY much for all of your help! You guys are truly terriffic here at TSDN! If I have a problem, I will post back. Thanks again.
Sep 8 '07 #13
Atli
5,058 Expert 4TB
Your very welcome. Glad I could help :)
See you around.
Sep 8 '07 #14
Rather than using all that code to see if a record exists you can simply use

ON DUPLICATE KEY UPDATE

example:

mysql_query ("INSERT INTO table (field1, field2) VALUES ('$value1','$value2') ON DUPLICATE KEY UPDATE field1='$value1', field2=CONCAT(field2, ' ', '$value2') ");

In this example I used CONCAT to add the new value to the old value in field2.

Good Luck.
Sep 12 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax:...
8
by: Sans Spam | last post by:
Greetings! I have a table that contains all of the function permissions within a given application. These functions are different sections of a site and each has its own permissions (READ, WRITE,...
2
by: Nick | last post by:
Loop to create an array from a dynamic form. I'm having trouble with an application, and I'll try to explain it as clearly as possible: 1. I have a form with two fields, say Apples and...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
5
by: eholz1 | last post by:
Hello PHP, I am having a problem. I know the area of the problem, but not how to solve it. It has to do with a php page with a form on it, and I am trying to perform an insert query into my...
7
by: bcap | last post by:
hi, I am trying to create a form where you may have more than one person at a meeting, but want to have them be related to the same meeting. I have a mulitple select text area and if you...
22
by: Lewe22 | last post by:
I am creating a small Access db which performs a series of updates to a SQL database. The Access db consists of a ‘Main Form’, from which the user can run each update via a series of command...
2
by: stamyo | last post by:
Hello to everyone, i'm facing the following problem: I have a form which is used for both insert or update data.. The situation is like that: when the user fills the form, press the submit button...
11
by: cooperkuo | last post by:
Dear all, I have a question about ADO in the subform. I know how to use ADO to insert/update/select data into the sigin form, but wehn I try to do it in the form with subform((Datasheet). I don't...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.