Jump to content

data integrity of data stored using drop down lists.


ajoo
Go to solution Solved by requinix,

Recommended Posts

Hi all, 

 

I just changed one of my forms to use drop down lists for storing Country, State, City and Pin values. 

 

The values for each of these fields come from individual tables in the database having the same name as these fields.

 

Since the values of pin or picodes are dependent on the city and their value in turn is dependent on the state and so on, so the tables need to check for data integrity.

 

However, the tables not only need to check for data integrity in the sense that a value should exist in the parent table, it also needs to be verified that the data comes from the correct subset of values.

 

For e.g.  The values for the fields are as shown in the table below,  parenthesis values representing their numeric unique ids. 

 

COUNTRY        STATE                  CITY              PIN

C1 (1)                ST1(1)                  CT1 (1)        11001 (1) 

                                                       CT2 (2)        11002 (2)

 

                          ST2(2)                  CT3 (3)        12001 (3)

                                                       CT4 (4)        12002 (4) 

 

 Then the valid sets of values are

 

CO  ST  CT   P

1--- 1 --- 1 --- 1      

1 ---1-----2----2

1----2-----3----3

1----2-----4----4

 

and so on.  

 

A value of 1 --- 2 --- 1 ---2  will obviously be wrong even though the values are coming from valid tables.

 

The question then is, how to ensure in the simplest possible way, that an entry that i fetch from the database or that I am going to insert into it is correct according to the subset values as well. 

 

Thanks all !

 

Link to comment
Share on other sites

Not sure I understand the problem...

 

State has a foreign key to the country, city has a foreign key to the state, pin has a foreign key to the city. Parent/child relationships.

 

When you get data from the form, look up the corresponding data from the three child tables and verify that the parent key matches what was given in the form. Like

$state = /* fetch state matching ST */
$city = /* fetch city matching CT */
$pin = /* fetch pin matching P */

if ($state["country"] == $CO && $city["state"] == $ST && $pin["city"] == $CT) {
	// valid
} else {
	// invalid
}
(obviously you should also check that the four values exist in the database in the first place - that overlaps a lot with this)
Link to comment
Share on other sites

Hi requinix, 

 

Thanks for the reply !

 

I think you understand the problem correct. As can be seen from your reply, It needs fetching data from the 3 tables and then making a comparison. What I would like to know is if it is possible to do this in a simpler manner ? Maybe it can be accomplished using a single mysql query. Yes ?

 

Thanks again !

Link to comment
Share on other sites

Hi requinix, 

 

 

 

Coupled with the WHERE based on the user's selection ...

 

I wonder if you mean the user id or the user's choice of the various fields because so far as I can see a WHERE clause on user's id makes no sense.  So I don't think it would need a WHERE clause. Kindly clarify.

 

 

Thanks loads. 

Link to comment
Share on other sites

  • Solution

User ID? I didn't say anything about a user ID...

 

SELECT whatever you want if anything at all
FROM countries co
JOIN states st ON co.id = st.country
JOIN cities ct ON st.id = ct.state
JOIN pins p ON ct.id = p.city
WHERE co.id = ? AND st.id = ? AND ct.id = ? AND p.id = ?
The query will only return a result if the four IDs exist and they are related to each other.
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.