This topic is locked
[SOLVED]

Add New Record + Prevent duplicate values

5/14/2025 11:29:57 AM
PHPRunner General questions
G
GMO author

Designer page, Prevent duplicate values, APPEARS that the search does not? search the DB table, but does search the DB table with the php query restrictions from WHERE stmts?

I have a table: "Clients" which has 20k records, however the table "clients" list view has 75 records due to WHERE stmts, due to (Active vs inactive checkbox),

While adding a new record: the field with "Prevent duplicate values" appears to search thru the 75 records vs the 20k records?

And the table continues to add new "client" records even though the client is in the DB Table.

Is the above program to do such or is there a switch I am missing in DESIGNER? Prevent duplicate values, search complete table vs filtered table?

Thank you for and and all feedback...

C
Chris Whitehead 5/14/2025

There's 2 ways you can do this.

First option is run a select query in the before add event to check if the record exists, the return false if a record exists.

Or add a unique index in the database, this is the best option as PHPRunner should handle the error when trying to add a duplicate record and you don't have to keep track of the code if you add more views.

G
GMO author 5/16/2025

Thank you Chris

I did the Unique index option you suggested.

Mysql stops or phpr stops the INSERT EVENT for new record.

But does not provide message as to what happened.

C
Chris Whitehead 5/16/2025

I expected PHPRunner to display a message stating why the record wasn't added as there's no failed event handler for the add events. Maybe this could be added in a future version.

If you need a message then you'll need to check if the record exists in the before record added event using the "Check if specific record exists"

G
GMO author 6/4/2025

I found a solution in this forumand help manuals.

The below script verifies there is no existing address, home phone, cell phones when adding "New Client".

It works for me, but could be much better. It should pop open a message box, open the existing record @ View Page, Yes? or No?

// =============================================================Begin
// I am using the below script at: Add page: Before record added.

// Script begins
global $conn;
$sql = "SELECT * FROM tblClients WHERE address ='".$values["address"]."' AND address <> '' ";
$rsExists = db_query($sql, $conn);
$data=db_fetch_array($rsExists);
if($data) {
$message = "address ".$values["address"]." in database.";
return false;
}
// ============================================================== End

// ============================================================== begin
global $conn;
$sql = "SELECT * FROM tblClients WHERE PhHome ='".$values["PhHome"]."' AND PhHome <> '' ";
$rsExists = db_query($sql, $conn);
$data=db_fetch_array($rsExists);
if($data)
{
$message = "PhHome ".$values["PhHome"]." in database.";
return false;
}
// ============================================================== End

// ============================================================== begin
global $conn;
$sql = "SELECT * FROM tblClients WHERE PhCellMs ='".$values["PhCellMs"]."' AND PhCellMs <> '' ";
$rsExists = db_query($sql, $conn);
$data=db_fetch_array($rsExists);
if($data)
{
$message = "PhCellMs ".$values["PhCellMs"]." in database.";
return false;
}
// ============================================================== End

// ============================================================== begin
global $conn;
$sql = "SELECT * FROM tblClients WHERE PhCellMr ='".$values["PhCellMr"]."' AND PhCellMr <> '' ";
$rsExists = db_query($sql, $conn);
$data=db_fetch_array($rsExists);
if($data)
{
$message = "PhCellMr ".$values["PhCellMr"]." in database.";
return false;
}
// ============================================================== End
return true;