Tech Musings

Tuesday, April 18, 2006

PHP, MySQL and duplicate entry error message

I recently created an online form application for our VISIONS professional development program that automatically inserts users' information into a database. The unique identifier for the application is a teacher's user number. If an application had already been submitted and was on file for that user, I wanted to return a message that read "prettier" than the standard duplicate entry 'whatever' for key some number.

So, I created a query that searched for an entry that matched the POSTED teacher id (the potential duplicate) and returned a die ("It looks like we already have an app on file...") message if the query held a value. If no entry was found, the script continue on its merry way and completed the INSERT into the database.

$colname_get_tid = "1";
$colname_get_tid = $_POST['tid'];

mysql_select_db($database_visions, $visions);
$query_get_tid = sprintf("SELECT tid, stamp FROM app WHERE app.tid = '%s'", $colname_get_tid);
$get_tid = mysql_query($query_get_tid, $visions);
$row_get_tid = mysql_fetch_assoc($get_tid);
$totalRows_get_tid = mysql_num_rows($get_tid);

if ($totalRows_get_tid) {
die ("It looks like we already have an application on file for
teacher $colname_get_tid that was submitted on $time1, $time2 at $time3. Have you completed this application before? Please call us to check on the status or your application, or click the browser's back button and try again.");
}

else do the INSERT...

0 Comments:

Post a Comment

<< Home