Skip to main content

Data Validation with MySQL Triggers

One of the applications that I get to play with on a regular basis is a PHP, Javascript AJAX environment that acts as a frontend for a MySQL database. I wanted to add range constraints to some of the fields in order to reduce typo's.

I created before triggers with the following basic skeleton for each table that I wanted to add the range constraint to.

if NEW.columnname > theupperrange then
SELECT `**columnname is out of range**` from thetable into @str_val;
end if;

This will cause the insert to fail with the follow error message
Unknown column `**columnname is out of range**`

because of the nature of the application (if an error occurs in MySQL the PHP returns the error message) it then becomes the JavaScript's XMLHttprequest.responseText. so on the javascript side of the application i look for the Unknown column like this

//req is the name of the XMLHttpRequest object i create to handle interaction between
//the javascript and PHP.

if(req.responseTest.indexOf("Unknown column"))
{
msg = req.responseText
arr_msg = msg.split("**")
themsg = arr_msg[1]
alert(themsg)
//window1 is where data is entered, I know it's a real meaningful name:)
window1.focus()
}

else
{
regular application stuff
}

The idea of how to write the trigger was not original with me. I got it from http://www.brokenbuild.com/blog/category/mysql/ , who I believe said he got it from another source.

Comments

Popular posts from this blog

2013 BeeBumble 10k: Race Report

This has quickly become one of my favorite races of the year and I am not alone because there was a record 575 participants across the different events this year. I like the small town atmosphere, I like the course that is an out and back on a country road, and the timing makes it a perfect tune up race for the fall season. After my performance here the last two years I am also really beginning to appreciate it for the confidence booster it is and it's ability to add a breath of fresh air to the end of the training cycle for the 2013 Indianapolis Monumental Marathon. For this race I added an early morning shake out run to get my body primed for running. This was just a short and easy 20 minute run very first thing out of bed. Nearly all of my running is first thing in the morning but  I've never done it as a shakeout for a race. I liked it and think I'll keep it. The last 5k I ran I didn't do a warm up and I learned my lesson because it cost me a very slow fi...

My AMDG Run: An Answered Prayer.

I have never been a person to pray for specific favors. It never felt right to me. My prayers generally are for strength, guidance, an acceptance of what comes my way. That is always how I "felt" right praying. To pray for a specific favor like a good grade, to do well in some competition and things like that just always felt a little wrong and to pray for physical healing was something I never would have considered. I am not saying this was any kind of selflessness. I am not sure exactly what it was. A fear of seeming childish or perhaps it was out of fear that my faith would be shaken if the prayer wasn't answered. Regardless, this is how I generally pray. And that is not to say that I don't or didn't think that God doesn't concretely answer prayers. I believe that He does. For instance I have always prayed when overwhelmed something like "God, I cannot do this all alone, please help me" and I will somehow, someway find my way through whatever it...

Comparisons to Hitler

Hitler was an evil man and you know that from the fruit of his work, the mass annihilation of ethnic groups. He was pure evil. He killed people simply for their Israelite heritage. That being the case, it's only natural in the modern American political landscape that from time to time you see various politicians likened to Hitler by their detractors. After his comments on general immigration and specifically Muslim immigrants and visitors, Donald Trump is the latest target of the "Hitler Attack"and his supporters are likened to Hitler's supporters. Don't get me wrong, I'm no Trump supporter. I think the guy is a joke and frankly, I wish he would sit down and shut up. However, to compare him to Hitler; that's wrong. It is as wrong to the Jewish people, the cripples, the infirm, and the rest of the "useless" people who suffered under the Nazi regime as it is to Trump himself. If you really feel the need to compare someone or something in Am...