Forums

Check duplicate person in database

hv 27 Nov, 2014
Hi!
I'm developing a form with a external database. The primary key is id ,but we have another field, dni (identity card) and is unique.
I need check if that person inserted in my database was already inserted or not.
How I can check it? I'm beginner in Joomla.

Thank you!
Bye!!
BNDragon 28 Nov, 2014
Hi hv,

Try a Custom code, check with a php code if he already exists.

BN
hv 01 Dec, 2014
Hi BN,
Should I have a custom code before or after DB SAVE?
Don't I need reading my database?
And, do you have a code example?

Thank you for your answer!!
BNDragon 01 Dec, 2014
Hi hv,

I would switch the db save for a custom action, so I'll do it with some like:
(p.s. I'm assuming that your user is logged)
<?php
$user = JFactory::getUser();
$con=mysqli_connect("server, "username", 
"password", "databasename");
// Check connection
if (mysqli_connect_errno())
{
echo "Error: " . mysqli_connect_error();
}
$userExists= mysqli_query($con,"SELECT * FROM 
"tablename" WHERE dni=".$user->get("id")."");

if (!$userExists) { 
$query= mysqli_query($con,"INSET INTO "tablename" ('dni', 'field1', 'field2', 'field3', 'field4', 'fieldN') VALUES ('".$user->get("id")."',"".$form->data["field1"]."","".$form->data["field2"]."","".$form->data["field3"]."","".$form->data["field4"]."","".$form->data["fieldN"]."")");
 }
?>


I dind't test the code, so please, test it and debug if needed.

BN
inocampo 09 Dec, 2014
1 Likes
BNDragon

Good idea, but you can use "Event Switcher", custom Code for check if already exist the user with the Joomla API for less code.


$db = JFactory::getDbo();
$user = JFactory::getUser();
 
$query = $db->getQuery(true);
$query= 'SELECT * FROM #__tablename WHERE id='. $user->get("id");
$db->setQuery($query);
$db->query();
$num_rows = $db->getNumRows();

if ($num_rows==0){
    return "success";
    }else{
    return "fail";
}


In success option, you load the action "DB Save" and de Fail Option you load the action "Event Loop"

See the image http://prntscr.com/5esdgc

Try, good day
BNDragon 09 Dec, 2014
Hi inocampo,

That's a great idea, I didn't remember that😉

BN
This topic is locked and no more replies can be posted.