close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

How to check two unique value in the same record of the database?

Thread began 9/10/2018 8:45 am by tony | Last modified 10/17/2018 8:47 am by Ray Borduin | 1115 views | 11 replies |

tony

How to check two unique value in the same record of the database?

Hello all,
I have a form that has two fields that I want to validate. This couple of fields must be unique.
So, how can I validate them?
Have I to set them individually in the WA Server validation SB?
Then I set a conditional region to show only when the two fields do not validate?
I'm trying to do so, but the WA fails even one of the two fields differs.
How can check two fields to be unique during a single insert operation?
The validation should fail only when the two fields appear to be already inserted in the db in another record.
TIA
tony

Sign in to reply to this post

Ray BorduinWebAssist

There is no validation that will work like that out of the box. I think a good solution might be to add a recordset to the page and filter it by the two values and then you can validate that the number of records returned is zero.

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

tony

Thanks for the reply, Ray,
I created the recordset, (rs_duplicated_fields) so that it will filter the DB table by two fields.
Then I created a WA server validation using the NUMBER rule. I set the validate field with $totalRows_rs_duplicated_fields and set the rule to minimum -1 and maximum to 0.
I moved the validation code under all my recordsets code and it seems to work now.
TIA
tony

Sign in to reply to this post

tony

Hi Ray,
I created a new website and implemented the same solution, but this time it doesn't work.
I'll try to explain:
now I need to compare at least 3 fields.
So, I created a recordset that should display the records that will have these 3 fields identical.

This is the recordset I use:

SELECT *
FROM registrazioni
WHERE registrazioni.data_scontrino_reg = datascontrino AND registrazioni.ora_scontrino_reg=orascontrino AND registrazioni.minuti_scontrino_reg = minutiscontrino AND registrazioni.numero_scontrino_reg = numeroscontrino



All variables here are compared to the related $_POST variables (as TEXT).

The strange thing is that it works locally (on a qnap webserver that has php 5.5.38), but it doesn't online (PHP 5.6.38).
Online, the validation pass everytime.
Please note that all other fields will validate correctly.

What can I check to make it work?
TIA
tony

Sign in to reply to this post

Ray BorduinWebAssist

The recordset is probably not returning a result. To test that, write the SQL to the page with:

<?php echo($query_rs_doppioni); ?>

Then test that in phpMyAdmin to see what it wrong and how it needs to be adjusted to return the result when you need it to. My guess is that your date field syntax needs to be updated, but I can't tell for sure without looking at the database and query like I'm advising you to.

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

tony

Hi Ray,
this is something I already tried. I use Navicat for MySQL to run queries directly on the final server.
Here is the query I use to make this test:

SELECT *
FROM registrazioni
WHERE
registrazioni.data_scontrino_reg = '2018-10-03' AND
registrazioni.ora_scontrino_reg = '15' AND
registrazioni.minuti_scontrino_reg = '50' AND
registrazioni.numero_scontrino_reg = '528'


The query results are ok. Some records are displayed. But the validation still (erroneously) pass.
So, the WA validation server behaviour should fail (since $totalRows_rs_doppioni is a number >0).
I attach the WA server validation screenshot related to this check: do you think it is correct?
TIA
tony

Sign in to reply to this post

tony

Ok I found the culprit (date field: you were right about it, Ray) because I tried to do the same validation excluding the date field from the recordset.
I solved definitely changing the code of the recordset from:

$datascontrino_rs_doppioni = "-1";
if (isset($_POST['data_scontrino_reg'])) {
$datascontrino_rs_doppioni = $_POST['data_scontrino_reg'];
}
$orascontrino_rs_doppioni = "-1";
if (isset($_POST['ora_scontrino_reg'])) {
$orascontrino_rs_doppioni = $_POST['ora_scontrino_reg'];
}
$minutiscontrino_rs_doppioni = "-1";
if (isset($_POST['minuti_scontrino_reg'])) {
$minutiscontrino_rs_doppioni = $_POST['minuti_scontrino_reg'];
}
$numeroscontrino_rs_doppioni = "-1";
if (isset($_POST['numero_scontrino_reg'])) {
$numeroscontrino_rs_doppioni = $_POST['numero_scontrino_reg'];
}
mysql_select_db($database_conn_registrazione, $conn_registrazione);
$query_rs_doppioni = sprintf("SELECT * FROM registrazioni WHERE registrazioni.data_scontrino_reg = %s AND registrazioni.ora_scontrino_reg = %s AND registrazioni.numero_scontrino_reg = %s AND registrazioni.minuti_scontrino_reg = %s", GetSQLValueString($datascontrino_rs_doppioni, "text"),GetSQLValueString($orascontrino_rs_doppioni, "text"),GetSQLValueString($numeroscontrino_rs_doppioni, "text"),GetSQLValueString($minutiscontrino_rs_doppioni, "text"));
$rs_doppioni = mysql_query($query_rs_doppioni, $conn_registrazione) or die(mysql_error());
$row_rs_doppioni = mysql_fetch_assoc($rs_doppioni);
$totalRows_rs_doppioni = mysql_num_rows($rs_doppioni);


to:

$datascontrino_rs_doppioni = "-1";
if (isset($_POST['data_scontrino_reg'])) {
$datascontrino_rs_doppioni = date('Y-m-d',strtotime(str_replace("/","-",$_POST["data_scontrino_reg"])));
}
$orascontrino_rs_doppioni = "-1";
if (isset($_POST['ora_scontrino_reg'])) {
$orascontrino_rs_doppioni = $_POST['ora_scontrino_reg'];
}
$minutiscontrino_rs_doppioni = "-1";
if (isset($_POST['minuti_scontrino_reg'])) {
$minutiscontrino_rs_doppioni = $_POST['minuti_scontrino_reg'];
}
$numeroscontrino_rs_doppioni = "-1";
if (isset($_POST['numero_scontrino_reg'])) {
$numeroscontrino_rs_doppioni = $_POST['numero_scontrino_reg'];
}
mysql_select_db($database_conn_registrazione, $conn_registrazione);
$query_rs_doppioni = sprintf("SELECT * FROM registrazioni WHERE registrazioni.data_scontrino_reg = %s AND registrazioni.ora_scontrino_reg = %s AND registrazioni.numero_scontrino_reg = %s AND registrazioni.minuti_scontrino_reg = %s", GetSQLValueString($datascontrino_rs_doppioni, "text"),GetSQLValueString($orascontrino_rs_doppioni, "text"),GetSQLValueString($numeroscontrino_rs_doppioni, "text"),GetSQLValueString($minutiscontrino_rs_doppioni, "text"));
$rs_doppioni = mysql_query($query_rs_doppioni, $conn_registrazione) or die(mysql_error());
$row_rs_doppioni = mysql_fetch_assoc($rs_doppioni);
$totalRows_rs_doppioni = mysql_num_rows($rs_doppioni);


Hope this help someone else.
Tony

Sign in to reply to this post

tony

Hi Ray,
sorry to re-open this thread, but I upgraded the page to DataBridge 2 and I have a little problem:
the problem is still this: trying to block users that enter the same data more than a time ("unique database value" sort of, but checking 4 fields).
So, I entered the WA server validation sb and it has been automatically updated.
See attachment 1.

But validation pass even if I insert the same data.

Here is the recordset I would like to use and check:

<?php
$rs_doppioni = new WA_MySQLi_RS("rs_doppioni",$conn_registrazione_i,1);
$rs_doppioni->setQuery("SELECT * FROM registrazioni WHERE registrazioni.ora_scontrino_reg = ? AND registrazioni.data_scontrino_reg = ? AND registrazioni.minuti_scontrino_reg = ? AND registrazioni.numero_scontrino_reg = ?");
$rs_doppioni->bindParam("s", "".((isset($_POST["ora_scontrino_reg"]))?$_POST["ora_scontrino_reg"]:"") ."", "-1"); //orascontrino
$rs_doppioni->bindParam("s", "".((isset($_POST["data_scontrino_reg"]))?$_POST["data_scontrino_reg"]:"") ."", "-1"); //datascontrino
$rs_doppioni->bindParam("s", "".((isset($_POST["minuti_scontrino_reg"]))?$_POST["minuti_scontrino_reg"]:"") ."", "-1"); //minutiscontrino
$rs_doppioni->bindParam("s", "".((isset($_POST["numero_scontrino_reg"]))?$_POST["numero_scontrino_reg"]:"") ."", "-1"); //numeroscontrino
$rs_doppioni->execute();
?>



I tried the code above but it always validate.
So I made the same changes I made in databsridge 1:

<?php
$rs_doppioni = new WA_MySQLi_RS("rs_doppioni",$conn_registrazione_i,1);
$rs_doppioni->setQuery("SELECT * FROM registrazioni WHERE registrazioni.ora_scontrino_reg = ? AND registrazioni.data_scontrino_reg = ? AND registrazioni.minuti_scontrino_reg = ? AND registrazioni.numero_scontrino_reg = ?");
$rs_doppioni->bindParam("s", "".((isset($_POST["ora_scontrino_reg"]))?$_POST["ora_scontrino_reg"]:"") ."", "-1"); //orascontrino
$rs_doppioni->bindParam("s", "".((isset(date('Y-m-d',strtotime(str_replace("/","-",$_POST["data_scontrino_reg"])))))?date('Y-m-d',strtotime(str_replace("/","-",$_POST["data_scontrino_reg"]))):"") ."", "-1"); //datascontrino
$rs_doppioni->bindParam("s", "".((isset($_POST["minuti_scontrino_reg"]))?$_POST["minuti_scontrino_reg"]:"") ."", "-1"); //minutiscontrino
$rs_doppioni->bindParam("s", "".((isset($_POST["numero_scontrino_reg"]))?$_POST["numero_scontrino_reg"]:"") ."", "-1"); //numeroscontrino
$rs_doppioni->execute();
?>



It still validate even if I insert the same data.

I have already tried to remove the DATE field, but validation pass too...


Is this right or I need to use another method in DB2?

TIA
tony

Sign in to reply to this post

Ray BorduinWebAssist

This is the correct technique, but you shouldn't need to add the reformatting in DataBridge 2. You just change the data type to "t" like:

php:
<?php

$rs_doppioni 
= new WA_MySQLi_RS("rs_doppioni",$conn_registrazione_i,1);
$rs_doppioni->setQuery("SELECT * FROM registrazioni WHERE registrazioni.ora_scontrino_reg = ? AND  registrazioni.data_scontrino_reg = ? AND  registrazioni.minuti_scontrino_reg = ?  AND  registrazioni.numero_scontrino_reg = ?");
$rs_doppioni->bindParam("t""".((isset($_POST["ora_scontrino_reg"]))?$_POST["ora_scontrino_reg"]:"")  ."""-1"); //orascontrino
$rs_doppioni->bindParam("t""".((isset($_POST["data_scontrino_reg"]))?$_POST["data_scontrino_reg"]:"")  ."""-1"); //datascontrino
$rs_doppioni->bindParam("t""".((isset($_POST["minuti_scontrino_reg"]))?$_POST["minuti_scontrino_reg"]:"")  ."""-1"); //minutiscontrino
$rs_doppioni->bindParam("t""".((isset($_POST["numero_scontrino_reg"]))?$_POST["numero_scontrino_reg"]:"")  ."""-1"); //numeroscontrino
$rs_doppioni->execute();
?>



What does the validation code look like? Maybe that is where the mistake is.

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

tony

Originally Said By: Ray Borduin
  This is the correct technique, but you shouldn't need to add the reformatting in DataBridge 2. You just change the data type to "t" like:

php:
<?php

$rs_doppioni 
= new WA_MySQLi_RS("rs_doppioni",$conn_registrazione_i,1);
$rs_doppioni->setQuery("SELECT * FROM registrazioni WHERE registrazioni.ora_scontrino_reg = ? AND  registrazioni.data_scontrino_reg = ? AND  registrazioni.minuti_scontrino_reg = ?  AND  registrazioni.numero_scontrino_reg = ?");
$rs_doppioni->bindParam("t""".((isset($_POST["ora_scontrino_reg"]))?$_POST["ora_scontrino_reg"]:"")  ."""-1"); //orascontrino
$rs_doppioni->bindParam("t""".((isset($_POST["data_scontrino_reg"]))?$_POST["data_scontrino_reg"]:"")  ."""-1"); //datascontrino
$rs_doppioni->bindParam("t""".((isset($_POST["minuti_scontrino_reg"]))?$_POST["minuti_scontrino_reg"]:"")  ."""-1"); //minutiscontrino
$rs_doppioni->bindParam("t""".((isset($_POST["numero_scontrino_reg"]))?$_POST["numero_scontrino_reg"]:"")  ."""-1"); //numeroscontrino
$rs_doppioni->execute();
?>


What does the validation code look like? Maybe that is where the mistake is.  



Hi Ray,
please consider the DATE is collected using the datepicker in the dd/mm/yyyy format.
Do DB2 take care of this and convert the date from dd/mm/yyyy to yyyy-mm-dd (database format) on the fly?

Anyway, here is the recordset code and the WA code related to it:

php:
<?php

$rs_doppioni 
= new WA_MySQLi_RS("rs_doppioni",$conn_registrazione_i,1);
$rs_doppioni->setQuery("SELECT registrazioni.data_scontrino_reg, registrazioni.ora_scontrino_reg, registrazioni.minuti_scontrino_reg, registrazioni.numero_scontrino_reg FROM registrazioni WHERE registrazioni.data_scontrino_reg = ? AND registrazioni.ora_scontrino_reg = ? AND registrazioni.minuti_scontrino_reg = ? AND registrazioni.numero_scontrino_reg = ?");
$rs_doppioni->bindParam("t""".(isset($_POST['data_scontrino_reg'])?$_POST['data_scontrino_reg']:"")  ."""2018-10-16"); //WAQB_Param1
$rs_doppioni->bindParam("s""".(isset($_POST['ora_scontrino_reg'])?$_POST['ora_scontrino_reg']:"")  ."""10"); //WAQB_Param2
$rs_doppioni->bindParam("s""".(isset($_POST['minuti_scontrino_reg'])?$_POST['minuti_scontrino_reg']:"")  ."""15"); //WAQB_Param3
$rs_doppioni->bindParam("s""".(isset($_POST['numero_scontrino_reg'])?$_POST['numero_scontrino_reg']:"")  ."""100"); //WAQB_Param4
$rs_doppioni->execute();
?>



Note: the inserted values above ("2018-10-16" and so on) are only temporary; I use them just to be sure the query works. Then, in production, I subtituite them with "-1".

validation:

php:
$WAFV_Errors .= WAValidateNM($rs_doppioni->TotalRows . "",-1,0,""," ,",true,17);



TIA
tony

Sign in to reply to this post
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...