multiple-columns-with-unique-data

Questionsmultiple-columns-with-unique-data
Jeff asked 3 days ago

I understand the validation code on https://www.gridphp.com/demo/demos/editing/server-validation.phps

However, what if I have multiple columns that cannot contain duplicate data? Using the code above as an example, what is the most efficient code to verify that both the name and company form input data does not already exist in the database?

For example, if a user was to submit the insert form, we need to check that John Doe does not already exist in the name column and that Walmart does not already exist in the company column. Can this be done with a single connection/check of the database?

I’m assuming that I can use the same exact code for the edit_client function, also. Is this true?

7 Answers
Abu answered 2 days ago

Hello,

You can simply apply AND condition to this sample query.

Rest code will be same, i.e. if these is a record found it would show error message.
And yes, you can use sample callback function for add and edit operation.
Sample code checks for a row with exact name and company combination entered again.

$e["on_insert"] = array("validate_data", null, true);
$e["on_update"] = array("validate_data", null, true);
$g->set_events($e);

function validate_data($data)
{
	$check_sql = "SELECT count(*) as c from clients where LOWER(`name`) = '".strtolower($data["params"]["name"])."' AND LOWER(`company`) = '".strtolower($data["params"]["company"])."'";

	$rs = $g->get_one($check_sql);
	if ($rs["c"] > 0)
		phpgrid_error("Person already exist in database");
}

For more details you can refer events docs.
https://www.gridphp.com/docs/grid-events.html

Jeff answered 2 days ago

Thank you for the example but it lets me add a record with a duplicate name OR company.

The “Person already exist in database” error will display when I attempt to add a record with a duplicate name AND company.

The “Person already exist in database” error will display if I attempt to add a record containing a name OR company that already exists in two or more records.

 

Jeff answered 2 days ago

Also, it will not allow you to update a record unless you change the name OR company, before submitting, since the record you are editing already exists.

Maybe it cannot be done when updating a record. I was thinking a user could update an existing record and keep the same name and/or company.

Jeff answered 2 days ago

Also, global $g; needs to be added to the function.

Jeff answered 21 hours ago

… $check_sql = \”SELECT count(*) as c from clients where LOWER(`name`) = \’\”.strtolower($data[\”params\”][\”name\”]).\”\’ AND LOWER(`company`) = \’\”.strtolower($data[\”params\”][\”company\”]).\”\’\”; … I\’ve tried several variations of the code above but I just cannot get it to work. It let\’s me add one duplicate before it triggers the error.   The code below works and disallows a duplicate name but does not provide a way to check for duplicate company: … $check_sql = \”SELECT count(*) as c from clients where LOWER(`name`) = \’\”.strtolower($data[\”params\”][\”name\”]).\”\’\”; …

Jeff answered 21 hours ago

*** Please delete my previous post above due to lack of readability *** Not sure what happened to format.


$check_sql = "SELECT count(*) as c from clients where LOWER(`name`) = '".strtolower($data["params"]["name"])."' AND LOWER(`company`) = '".strtolower($data["params"]["company"])."'";

I’ve tried several variations of the code above but I just cannot get it to work. It let\’s me add one duplicate before it triggers the error. The code below works and disallows a duplicate name but does not provide a way to check for duplicate company:


$check_sql = "SELECT count(*) as c from clients where LOWER(`name`) = '".strtolower($data["params"]["name"])."'";

Jeff answered 4 hours ago

As a workaround, I tried setting the name and company columns to Unique in the database with NULL as the default. However, if you enter a new record with the name or company field blank, NULL is not added in the database. Then if you try to enter a second record with a blank company field, you get a database error that it’s a duplicate because the empty fields are not triggering the default NULL. In other words, NULL is missing, for company, in the record that was submitted with the blank field.

I really could use some help 🙂

Your Answer

8 + 6 =

Login with your Social Id:

OR, enter

Attach code here and paste link in question.
Attach screenshot here and paste link in question.



How useful was this discussion?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate it.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?