What's new

Closed validation in database please help

Status
Not open for further replies.

Kulotski02

Eternal Poster
Joined
Jan 27, 2017
Posts
1,110
Reaction
1,236
Points
508
hi master dyan pa tulong naman

meron kasi ginagawang website hehehe

meron akong 2 columns INT sya, paano ba mag kakaroon ng validation na

kapag pareho ng value si column 1 at column 2 hindi na sya mag iinsert sa database

patulong namna mga sir!! salamat sa mga tutulong
 
Do the validation on the UI AND the backend.

You don't want the validation to be on the UI only because a smart wannabe häçker can just bypass the UI and go directly to the request and replay it without using the UI.

You want a validation on the UI for better user experience and you don't want to spend that extra round trip to the backend just to know a simple mismatch of input has happened. It's faster if they don't go to the backend for validation.

But you still need to add the validation on the backend because your UI can be bypassed and you want your backed to be reusable without the UI. That means it needs to encapsulate the same validation.
 
sakit sa ilong ng english ahahaha, salamat reply sir. paanong codes kaya gagawin ko dun kung sa backend ko kukunin yung ivavalidate ko, hirap kasi kanina pa ako nag hahanap kung paano hahaha
 
sakit sa ilong ng english ahahaha, salamat reply sir. paanong codes kaya gagawin ko dun kung sa backend ko kukunin yung ivavalidate ko, hirap kasi kanina pa ako nag hahanap kung paano hahaha

Sorry I'm in the US. I just kinda feel it's easier to express my thoughts via English instead of Tagalog. (I still know Tagalog and speak it well but when writing I find English to be easier for me. Kinda like how some people find VisualBasic easy but I find it clunky.) Also most documentation out there is written in English.

What is your backend? Do you use an ORM along with your DB?

The pseudocode is something like

Code:
data = get record from db
if data.col1 != data.col2
      do something with data
      upsert data in db
 
If you are using Java/Spring/Spring Data JPA, an example implementation is:

Code:
@Component
public class CarrierService {
  
  @Autowired
  private CarrierRepository carrierRepository;
  
  public void update(String id, String city) {
    Carrier existingCarrier = carrierRepository.findById(id).orElse(null);
    if (existingCarrier != null) {
      existingCarrier.updateCity(city);
      carrierRepository.save(existingCarrier);
    }
  }
}
 
If you want to check that they are not duplicate, you can create a method that just compares if they are duplicate

Code:
if (!existingCarrier.isDupe(city)) {
  existingCarrier.updateCity(city);
  carrierRepository.save(existingCarrier);
}
 
Hahahaha sakit ng ulo. Pero nagegets ko konti lang hahaha salamat mga sir medyo nag kakaroon ako ng idea. Pero sobrang slight lang hahaha
 
How do you get a record from the db?

Can you show me a code snippet of your backend service that handles this?
 
Para makapag-provide ng more accurate recommendations ang mga tao dito, mas mainam siguro if you can provide an schema/design of your DB and the purpose of your website. At rationale ng mutual exclusivity between 2 columns.
 
Are you using ORM? if yes, then you might want to use NHibernate.Validators if you use Nhibernate for instance. If you use Microsoft product specifically ASP.Net using Entity Framework then you can use Data Annotations. If you dont use any of these, you can use a simple UI validation then verify it in your Database upon saving via stored procedure or LINQ query if you use visual studio.

The idea is, validate it from the ui before saving, then do the final verification in the DB.
 
Sobrang simple lang ng data base ko.

Add edit delete lang with 3 columns 1st columns is name 2nd column naman is insert ng number 3rd insert ulit ng number.

Ang gusto ko po sana ay pag meron nang na insert na number ex c1=value is 2 c2=value is 3
If may try ulit ako ng mag insert kay c1 ng 2 at c2 ng 3 hindi na sya kag iinsert sa database.

Ganto po sana
 
ah so ang sinasabi mo pala, duplication sa row sa table mo sa database? for example, may record ka ng Col 1 Juan Col 2 Tamad so dapat pag may nag insert ng the same record hindi na dapat ma save? kung ganyan ibig mong sabihin, e di maglagay ka ng primary key mo para yun ang magiging basis mo kung existing na yun sa database mo. There are many ways to do that.

supposed you have a table Person:

EmpId Fname LastName

The Column "EmpId" should be the primary key. You can also add one more Column to further distinct each record. Something like, "RowId" that is set to auto increment and index as its type. But for this case, which is the employee table, this additional column is not necessary. You only want to add distinct column if you are constructing a table that holds a record with the same Id or model number, etc.

When you insert a new record, you can use IF EXISTS() statement in your stored procedure for inserting records. Or, you can make a query of record using the employee id that you are about to insert. If there are returned resultset, then it is already existing from the records, otherwise, you can add it as a new record. There's a lot of ways to solve your problem, you just have to think how you would do it.
 
I think one of the lessons here is to describe your problem as descriptive as possible. Look at StackOverflow for patterns how people ask programming related questions
 
I think one of the lessons here is to describe your problem as descriptive as possible. Look at StackOverflow for patterns how people ask programming related questions

Correct! people there are systematic about posting their queries and replies. Your post will be auto declined / purged if you do not conform to their guidelines. I wish this forum should tightly implement similar guidelines especially in this particular forum section.
 
Last edited:
Status
Not open for further replies.
Back
Top