This topic is locked
[SOLVED]

 Check if specific record exists

1/9/2017 4:23:12 PM
PHPRunner General questions
R
reimic author

hi,

I want use the following code in the Before record added event but it is don't work.

thank you for you help.
http://www.asprunner.com/forums/topic/24403-check-if-specific-record-exists/pageviewfindpostp81075

B
bencroftweb 1/9/2017



hi,

I want use the following code in the Before record added event but it is don't work.

thank you for you help.
//** Check if specific record exists ****

$strSQLExists = "select * from public.hr_conge where public.hr_conge.usager=$_SESSION["UserID"] and public.hr_conge.date ='" . $values["date"] . "'";

$rsExists = db_query($strSQLExists);

$data=db_fetch_array($rsExists);

if($data)

{

// if record exists do something

$message = "date " . $values["date"] . " already exists."

return false;

}

else

{

// if dont exist do something else

}


Agreed - This does not work. It's something around the return false;

B
bencroftweb 1/9/2017



Agreed - This does not work. It's something around the return false;


Doing a little searching on this forum I found a suggestion to remove the return false and put the semi colon after exists."
{

// if record exists do something

$message = "date " . $values["date"] . " already exists.";

}

else

{

// if dont exist do something else

}
Yet to try it but might be worth a whirl :-)

R
reimic author 1/10/2017



Doing a little searching on this forum I found a suggestion to remove the return false and put the semi colon after exists."
{

// if record exists do something

$message = "date " . $values["date"] . " already exists.";

}

else

{

// if dont exist do something else

}
Yet to try it but might be worth a whirl :-)


thanks

I have the problem with this line because it don't works :

$strSQLExists = "select * from public.hr_conge where public.hr_conge.usager=243 and public.hr_conge.date ='" . $values["date"] . "'";
this is my ref: https://asprunner.com/phprunner/docs/check_if_specific_record_exists.htm

R
reimic author 1/11/2017



thanks

I have the problem with this line because it don't works :

$strSQLExists = "select * from public.hr_conge where public.hr_conge.usager=243 and public.hr_conge.date ='" . $values["date"] . "'";
this is my ref: https://asprunner.com/phprunner/docs/check_if_specific_record_exists.htm



this line no woks also:

$strSQLExists = "select * from public.hr_conge where public.hr_conge.date ='" . $values["date"] . "'";

L
laonian 1/11/2017

Can you try this?
[size="2"]

$strSQLExists = "select * from public.hr_conge where public.hr_conge.date ='".date('Y-m-d',strtotime($values["date"]))."'";

[/size]
I suppose your database date field is in 'Y-m-d' format, e.g. "2017-01-11".

R
reimic author 1/13/2017

thanks but it's not ok
user must not duplicate the date for himself

for exemple:

tata must not be valid on the same date:060117
this is my tables:



credentials


id;user;login;pass

1;250;tata;passtata

2;255;tete;passtete

3;356;toto;passtoto
hr_conge

id;user_id;date

1;256;010117 it's ok

2;255;010117 it's ok

3;250;060117

4;250;060117 not possible for the same user
this is my wrong code
global $conn;

$strSQLExists1 = "select user from credentials where login=$_SESSION[UserID]";

$rsExists1 = db_query($strSQLExists1);

$data1=db_fetch_array($rsExists1);

$strSQLExists = "select * from hr_conge where user=$data1 and date ='".$values["date"]."'";

$rsExists = db_query($strSQLExists);

$data=db_fetch_array($rsExists);

if($data)

{

// if record exists do something

$message = "date ".$values["date"] ." already exists.";

}

else

{

// if dont exist do something else

}

L
laonian 1/13/2017

You have several errors in your query. I am not the expert, but try to provide you with some code. Try this and see if it works.
Event code before record added.


global $conn;

$strSQLExists1 = "select * from credentials where login='".$_SESSION["UserID"]."'";

$rsExists1 = db_query($strSQLExists1,$conn);

$data1=db_fetch_array($rsExists1);

$strSQLExists = "select * from hr_conge where user='".$data1["user"]."' AND date ='".date('mdy',strtotime($values["date"]))."'";

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

$data=db_fetch_array($rsExists);

if($data)

{

// if record exists return false

$message = "Date ".date('dmy',strtotime($values["date"]))." already exists.";

return false;

}


And your database date format is supposed to be in "dmy" format, e.g. 130117.

R
reimic author 1/14/2017



You have several errors in your query. I am not the expert, but try to provide you with some code. Try this and see if it works.
Event code before record added.


global $conn;

$strSQLExists1 = "select * from credentials where login='".$_SESSION["UserID"]."'";

$rsExists1 = db_query($strSQLExists1,$conn);

$data1=db_fetch_array($rsExists1);

$strSQLExists = "select * from hr_conge where user='".$data1["user"]."' AND date ='".date('mdy',strtotime($values["date"]))."'";

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

$data=db_fetch_array($rsExists);

if($data)

{

// if record exists return false

$message = "Date ".date('dmy',strtotime($values["date"]))." already exists.";

return false;

}


And your database date format is supposed to be in "dmy" format, e.g. 130117.


I LOVE YOU ALSHINE, it's works

very very THANKS ...