close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Record Insert only if field entry doesn't already exist

Thread began 3/12/2009 12:39 pm by tom92909 | Last modified 3/13/2009 12:04 pm by tom92909 | 10152 views | 10 replies |

tom92909Beta Tester

Record Insert only if field entry doesn't already exist

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

Sign in to reply to this post

Ray BorduinWebAssist

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.

Sign in to reply to this post
Did this help? Tips are appreciated...

tom92909Beta Tester

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|html|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);
}
}
?>

Sign in to reply to this post

Ray BorduinWebAssist

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);

Sign in to reply to this post
Did this help? Tips are appreciated...

tom92909Beta Tester

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,$WADbSearch1,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,$WADbSearch1,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

Sign in to reply to this post

Ray BorduinWebAssist

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.

Sign in to reply to this post
Did this help? Tips are appreciated...

tom92909Beta Tester

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");
}
}
?>

Sign in to reply to this post

Ray BorduinWebAssist

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.

Sign in to reply to this post
Did this help? Tips are appreciated...

tom92909Beta Tester

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,$WADbSearch1,false);
$getEmails = mysql_query($query_getEmails, $SQL_TABLE) or die(mysql_error());
$row_getEmails = mysql_fetch_assoc($getEmails);
$totalRows_getEmails = mysql_num_rows($getEmails);

Sign in to reply to this post

Ray BorduinWebAssist

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.

Sign in to reply to this post
Did this help? Tips are appreciated...
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...