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

The Gosh Darn Tuesday Morning 15-Miler

What can you say about a 15-mile Tuesday morning run? I can say a lot. I’ve had a lot of time to think about what I can say about a 15-mile Tuesday morning run on more 15-mile Tuesday Morning runs than I can count. You see, they are a main stay of the marathon training plan that I use. That plan being Pete Pfitzinger’s Advanced Marathoning 18 week 70-85 miles per week plan. The first thing I can say about the Tuesday morning 15-mile run is that it’s not a long run. Pete clearly defines long runs as starting at 16 miles. So, the 15-miler that begins so many Tuesdays of my life is not a long run. I mean, who ever heard of doing a “long run” on a Tuesday morning. That would just be insane. Being that it’s not a long run I can’t do all of the things that I do around a long run. I can’t obsess over the weather for the 15-miler, I can’t eat extra carbs in preparation for the 15-miler, I can’t expect my family to give me deference and make accommodations for me

2015 Valpo Half Marathon Race Report

This was my big tune-up race for the Indianapolis Monumental Marathon. I always run a half-marathon at this point in the build up to the Monumental to get a final big fitness boost, a reality check on where I am at fitness-wise and, if all goes well, probably the most important aspect is the confidence boost that I get. I got one heck of a confidence boost yesterday, 10/25/2015, at the Valpohalf Half-marathon in Valparaiso IN. Valparaiso is about 2 hours from home which is kind of right there on the line of driving on race morning or staying in a hotel the night before. This time we decided to get up and drive. Valparaiso is on central time which puts it an hour behind us. Meaning the 8:30 AM start was really a 9:30 AM start for me.  Making the decision to drive that much easier. I have been dealing with some issues on the top of my right foot, which is probably extensor tendinitis, for the last couple of weeks. I saw my soft-tissue guy last Friday. He worked on it some and got me

I Don't Get Anything out of Mass!

A homily that I heard once and some wisdom from an old friend I think really put the Mass and our participation in the Mass into perspective. I remember the homily began by the priest telling us that our parish had lost a family to a non-denominational church down the road. He said that this family actually came to him and said they were leaving and going to this other church for two reasons. The first was that the music was better. The second reason was because they didn't feel like they got anything out of Mass. At this point I remembered something a friend once said that made a lasting impact on me. She said essentially, "I don't come to Mass for the music or for the homily or for anything other than Jesus. I come because Jesus is here in the Eucharist." I wish that those people had known my friend. Because there is no where that you can go to be so close to our Lord as you are when you receive Holy Communion at Mass. The priest reiterated this sentiment and th