This topic is locked

Field Validation

3/15/2006 6:12:55 PM
PHPRunner General questions
M
mathis author

How can i validate a numeric ID against a field in another table?
I have user register in one table with a unique ID. Then I want them to fill out a survey that I built in another table. But in the survey table I want then to enter the ID and have it check to make sure it is in the registration table, if not give an error message.

Sergey Kornilov admin 3/16/2006

Hi,
you can do it using events.

See my example below.

Event for Survey table:

function BeforeAdd(&$values)

{

global $conn;

$strSQLExists = "select * from register_table where registerID='".$values["surveyID"]."'";

$rsExists = db_query($strSQLExists,$conn);

$data=db_fetch_array($rsExists);

if($data)

{

// if record exists do

//corresponding record exists in register table

return true;

}

else

{

// if dont exist do

echo "corresponding record doesn't exist in register table";

return false;

}

}



where registerID is field in Register table, surveyID is field in Survey table, you enter the data in.