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

Running Goals or Running Goal; What is it That I Want?

The other day I got all caught up in setting running goals for myself for the rest of the year. After the goals were set, I found myself starting to worry about how I was going to reach them. Wondering if there would be time to sufficiently prepare for them all. My goals were to run a 5k in under 20 minutes, to run a half marathon in 1:25 minutes and qualify for and run in the Boston marathon in 2014. At first, I thought that setting and meeting these goals would make me a more serious runner. In fact, I thought that to be the "serious" runner I want to be that I needed these goals. However, while I was running the other morning I realized that first of all, I was thinking about how to shift my training to meet my 5k goal. Then I began thinking about the right approach to a 1:25 half while training to qualify for Boston in a full marathon a month later. That is when I realized that the ancillary goals were starting to consume my focus and quite possibly i...

Carmel Marathon 2020 Weeks 2 & 3

Training for the Carmel Marathon on April 4th, 2020 in Carmel Indiana Goal: 2:51 Week 2 Runs: 8 Weekly Milage: 72 Cumulative Milage: 139 Workouts / Long Run: 17 with 8 at Marathon Pace Performance Management Chart for the week showing my running fitness progression from the beginning of the cycle through the current week. The main focus this week was just building my milage a little from last week, about 5 more miles, while being ready for my first MP workout. I am pretty focused right now and I struggled some last week with wanting to do more that the plan called for. I really wanted to add in a threshold workout. Marathon training is about discipline and long term thinking though. I could have added in some threshold miles and it probably would have been ok but doing the plan the way the plan is written will get me where I want to go and will keep me feeling on track and help me from wavering in the other direction as well. I'm doing the plan, period. I ran my fi...

The Rosary: Staying Focused

There are several obstacles that someone who has committed to pray the Rosary will face. Since the Rosary is a long prayer one of the biggest challenges I face is staying focused. If you have read my other posts or if you are just familiar with the prayer, you know that the mental contemplation of the Mysteries is the main focus of the prayer. So getting distracted really detracts from the prayer. Even if I am only praying 1 chaplet of the Rosary, it still takes about 20 minutes. In that amount of time your mind is bound to wander some. I have found several different things to help me when my mind begins to drift off to the worries of everyday life. The first is to understand that this is going to happen and be watching for it. If I am not vigilant I can get through a whole decade and realize I have not thought one single time about the Mystery that I should have been contemplating. So before I begin the prayer I make a conscious determination to be watchful of my wandering mind. O...