This topic is locked

checking a field before an add record

1/26/2009 8:18:54 AM
PHPRunner General questions
N
nickditrolio author

could someone help me fill out this 'before add record' script?

I have a field named week and a user who logged in and can only change his data. I want this person to add a record as long as he doesn't already have a record with a field that has the same week as one he is trying to enter.

for instance he already has a record with a field week="09/06/2009"

He goes to the Add record page and enters "09/06/2009" in the week field box. This is a lookup box linked to my 'week_list' table. It would be real nice if "09/06/2009" didn't even pop up in the first place.
//** Check if specific record exists ****

global $conn;

$strSQLExists = "select pick1, pick2, pick3 from picks where week =' ????? '";

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

$data=db_fetch_array($rsExists);

if($data)

{

// if record exists do something..do[color=#8B0000] I just return false

}

else

{

// if dont exist do something else

}
return true;
thank you.

J
Jane 1/26/2009

Hi,
entered values are in the $values array.

Here is a sample:

global $conn;

$strSQLExists = "select pick1, pick2, pick3 from picks where week ='".$values["week"]."'";

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

$data=db_fetch_array($rsExists);

if($data)

{

$message = "Wrong week";

return false;

}
return true;

N
nickditrolio author 1/26/2009

Jane, I had to add the field, "name" to the SQL statment since different people can add their data with the same week. So the SQL statment looks for a row with a week that the user picks and a name that he logs on with. Even if he picks a unique week, this statement always come back 'True'.. Di I and them correctly? How can I echo the SQL statment to see what it's doing?
global $conn;

$strSQLExists = "select name, week from picks where week='" .$values["week"]."' & name='".$values["name"]."'";

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

$data=db_fetch_array($rsExists);

if($data)

{

$message="Wrong Week";

echo "week=".$values["week"];

echo "name=".$values["name"];

return false;

}

else

{

return true;
Thanks for the help, Jane.

J
Jane 1/27/2009

Nick,
to debug code print SQL query before:

$strSQLExists = "select name, week from picks where week='" .$values["week"]."' and name='".$values["name"]."'";

echo $strSQLExists;