View Full Version : Record Insert only if field entry doesn't already exist
tom92909
03-12-2009, 12:39 PM
I am trying to figure out how to insert a new record into a table, but only if one of the fields in that table doesn't contain the same informtion already. The intent here is to eliminate duplicate data entries. So I need to validate or check to make sure that table1.field1 isn't the same as the data being entered into the new record in table1.field1 from the insert new record form page. I guess I could make the field unique in MySQL, but I'd like to have a formatted dialog error response instead.
Example: Email Address List
Insert new email address into new record into email table, only if the the new email address doesn't already exist in the email table.
I own both DataAssist Pro and the Validation Tool Kit.
Thanks in advance for your comments.
Tom
Ray Borduin
03-12-2009, 03:07 PM
Add a recordset to the page that is filtered by the inserted email field.
Then use sever validation to validate that the number of rows in that recordset is equal to zero.
You will have to manually move the recordset above the validation toolkit code for it to work properly.
tom92909
03-12-2009, 04:42 PM
Ok, I've obviously made a mistake. It's not working, but no errors either, but it is allowing duplicates to be written.
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_SQL_TABLE, $SQL_TABLE);
$query_getEmails = "SELECT emailaddress FROM emails WHERE emailaddress = '".$_POST['emailaddress']."'";
$getEmails = mysql_query($query_getEmails, $SQL_TABLE) or die(mysql_error());
$row_getEmails = mysql_fetch_assoc($getEmails);
$totalRows_getEmails = mysql_num_rows($getEmails);
?>
<?php
if (isset($_POST["Insert"])) {
$WAFV_Redirect = "";
$_SESSION['WAVT_addemailaddresses_Errors'] = "";
if ($WAFV_Redirect == "") {
$WAFV_Redirect = $_SERVER["PHP_SELF"];
}
$WAFV_Errors = "";
$WAFV_Errors .= WAValidateAN($row_getEmails['emailaddress'] ='0' . "",true,true,true,true,"",true,1);
if ($WAFV_Errors != "") {
PostResult($WAFV_Redirect,$WAFV_Errors,"addemailaddresses");
}
}
?>
<?php
$WADbSearch1_DefaultWhere = "WHERE 0=0";
if (!session_id()) session_start();
if (isset($_POST["Insert"])) {
$WADbSearch1 = new FilterDef;
$WADbSearch1->initializeQueryBuilder("MYSQL","1");
//keyword array declarations
//comparison list additions
$WADbSearch1->addComparison("emailaddress","".((isset($_POST["emailaddress"]))?$_POST["emailaddress"]:"") ."","AND","=",0);
//save the query in a session variable
if (1 == 1) {
$_SESSION["WADbSearch1_add_emailaddresses"]=$WADbSearch1->whereClause;
}
}
else {
$WADbSearch1 = new FilterDef;
$WADbSearch1->initializeQueryBuilder("MYSQL","1");
//get the filter definition from a session variable
if (1 == 1) {
if (isset($_SESSION["WADbSearch1_add_emailaddresses"]) && $_SESSION["WADbSearch1_add_emailaddresses"] != "") {
$WADbSearch1->whereClause = $_SESSION["WADbSearch1_add_emailaddresses"];
}
else {
$WADbSearch1->whereClause = $WADbSearch1_DefaultWhere;
}
}
else {
$WADbSearch1->whereClause = $WADbSearch1_DefaultWhere;
}
}
$WADbSearch1->whereClause = str_replace("\\''", "''", $WADbSearch1->whereClause);
$WADbSearch1whereClause = '';
?>
<?php
if (isset($_POST["Insert"])) // Trigger
{
$WA_connection = $SQL_TABLE;
$WA_table = "emails";
$WA_sessionName = "WADA_Insert_emails";
$WA_redirectURL = "add_emailaddresses.php";
$WA_keepQueryString = false;
$WA_indexField = "recid";
$WA_fieldNamesStr = "fname|lname|address|city|state|zip|emailaddress|ht ml|active";
$WA_fieldValuesStr = "".((isset($_POST["fname"]))?$_POST["fname"]:"") ."" . "|" . "".((isset($_POST["lname"]))?$_POST["lname"]:"") ."" . "|" . "".((isset($_POST["address"]))?$_POST["address"]:"") ."" . "|" . "".((isset($_POST["city"]))?$_POST["city"]:"") ."" . "|" . "".((isset($_POST["state"]))?$_POST["state"]:"") ."" . "|" . "".((isset($_POST["zip"]))?$_POST["zip"]:"") ."" . "|" . "".((isset($_POST["emailaddress"]))?$_POST["emailaddress"]:"") ."" . "|" . "".((isset($_POST["html"]))?$_POST["html"]:"") ."" . "|" . "".((isset($_POST["active"]))?$_POST["active"]:"") ."";
$WA_columnTypesStr = "',none,''|',none,''|',none,''|',none,''|',none,''| ',none,''|',none,''|',none,''|',none,''";
$WA_fieldNames = explode("|", $WA_fieldNamesStr);
$WA_fieldValues = explode("|", $WA_fieldValuesStr);
$WA_columns = explode("|", $WA_columnTypesStr);
$WA_connectionDB = $database_SQL_TABLE;
mysql_select_db($WA_connectionDB, $WA_connection);
if (!session_id()) session_start();
$insertParamsObj = WA_AB_generateInsertParams($WA_fieldNames, $WA_columns, $WA_fieldValues, -1);
$WA_Sql = "INSERT INTO `" . $WA_table . "` (" . $insertParamsObj->WA_tableValues . ") VALUES (" . $insertParamsObj->WA_dbValues . ")";
$MM_editCmd = mysql_query($WA_Sql, $WA_connection) or die(mysql_error());
$_SESSION[$WA_sessionName] = mysql_insert_id();
if ($WA_redirectURL != "") {
if ($WA_keepQueryString && $WA_redirectURL != "" && isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] !== "" && sizeof($_POST) > 0) {
$WA_redirectURL .= ((strpos($WA_redirectURL, '?') === false)?"?":"&").$_SERVER["QUERY_STRING"];
}
header("Location: ".$WA_redirectURL);
}
}
?>
Ray Borduin
03-13-2009, 07:18 AM
The line:
$WAFV_Errors .= WAValidateAN($row_getEmails['emailaddress'] ='0' . "",true,true,true,true,"",true,1);
should probably be:
$WAFV_Errors .= WAValidateNM($totalRows_getEmails . "",-1,0,"",",.",true,1);
tom92909
03-13-2009, 08:24 AM
Ray,
Good Morning and thank you for your prompt reply/assistance.
I made your recommended adjustment and I'm still getting the application to write the duplicate. I found however that my DataSearch has placed a questionable line that is erroring out.
setQueryBuilderSource($query_getEmails,$WADbSearch 1,false);
If I remove this line the script works, but it will write a duplicate. With the line in I get the following error.
Fatal error: Call to undefined function setquerybuildersource() in /public_html/add_emailaddresses.php on line 39
Below is the section related to the RecordSet with the offending Search line.
mysql_select_db($database_SQL_TABLE, $SQL_TABLE);
$query_getEmails = "SELECT emailaddress FROM mailmerge WHERE emailaddress = '".$_POST['emailaddress']."'";
setQueryBuilderSource($query_getEmails,$WADbSearch 1,false);
$getEmails = mysql_query($query_getEmails, $SQL_TABLE) or die(mysql_error());
$row_getEmails = mysql_fetch_assoc($getEmails);
$totalRows_getEmails = mysql_num_rows($getEmails);
As always your assistance is greatly appreciated.
Tom
Ray Borduin
03-13-2009, 08:40 AM
This error means that the include file for DataAssist search is either not present, or too low on the page.
I don't see the include in the code you pasted, so perhaps it is below... move it to the top of the page to clear up this error.
tom92909
03-13-2009, 09:14 AM
In all the edits, it appears that it got left out, or as I look closer, perhaps I never added it or understood how to construct it. I'm very comfortable with the client side validation setup, but this is my first use of the server side validation.
What I'm trying to limit would be to restrict duplicate email addresses from being written to the MySQL table. Below is my first attempt to construct the server side validation.
<?php
if (isset($_POST["Insert"])) {
$WAFV_Redirect = "";
$_SESSION['WAVT_addemailaddresses_Errors'] = "";
if ($WAFV_Redirect == "") {
$WAFV_Redirect = $_SERVER["PHP_SELF"];
}
$WAFV_Errors = "";
$WAFV_Errors .= WAValidateRT(((isset($_POST["emailaddress"]))?$_POST["emailaddress"]:"") . ""," $row_getEmails['emailaddress'] | $row_getEmails['emailaddress'] ",true,1);
if ($WAFV_Errors != "") {
PostResult($WAFV_Redirect,$WAFV_Errors,"addemailaddresses");
}
}
?>
Ray Borduin
03-13-2009, 09:58 AM
use:
$WAFV_Errors .= WAValidateNM($totalRows_getEmails . "",-1,0,"",",.",true,1);
instead of:
$WAFV_Errors .= WAValidateRT(((isset($_POST["emailaddress"]))?$_POST["emailaddress"]:"") . ""," $row_getEmails['emailaddress'] | $row_getEmails['emailaddress'] ",true,1);
I have done this in the past and it works... if you have problems with this, it means the problem is in how you are filtering the recordset.
tom92909
03-13-2009, 11:08 AM
Getting closer to the finish line I think. So you've identified that my problem is with my recordset filter.
$colname_getEmails = "-1";
if (isset($_POST['emailaddress'])) {
$colname_getEmails = (get_magic_quotes_gpc()) ? $_POST['emailaddress'] : addslashes($_POST['emailaddress']);
}
mysql_select_db($database_SQL_TABLE, $SQL_TABLE);
$query_getEmails = sprintf("SELECT emailaddress FROM mailmerge WHERE emailaddress = %s", GetSQLValueString($colname_getEmails, "text"));
setQueryBuilderSource($query_getEmails,$WADbSearch 1,false);
$getEmails = mysql_query($query_getEmails, $SQL_TABLE) or die(mysql_error());
$row_getEmails = mysql_fetch_assoc($getEmails);
$totalRows_getEmails = mysql_num_rows($getEmails);
Ray Borduin
03-13-2009, 11:20 AM
It looks correct to me.
add code below like:
die($totalRows_getEmails . " " . $query_getEmails);
That will tell you if it looks correct and is returning the expected result.
tom92909
03-13-2009, 12:04 PM
It's perfect. Thank you for your patience and your time.
vBulletin® v3.8.1, Copyright ©2000-2012, Jelsoft Enterprises Ltd.