This topic is locked
[SOLVED]

 Checking if Data Exists in another table in a button

10/17/2012 4:49:54 PM
PHPRunner General questions
C
copper21 author

Hello,
I created a button that inserts data into another table and it works great. I have this button on the list page. Is there a way to have this button check to see if a record exists in that other table before it inserts data?
This is the code I have for inserting data into another table in the "Server" tab of the button:
global $dal;
if($keys['ID'])

{
$tblEvents = $dal->Table("Other_Table");
$tblEvents->Value["ID2"]=$keys["ID"];
$tblEvents->Value["PID"]=$_SESSION["UserID"];
$tblEvents->Value["person_like"]=1;
$tblEvents->Add()
}
Is it possible to use something like this?
global $conn;

$strSQLExists = "SELECT ID, PID FROM Other_Table WHERE PID = ".$_SESSION["UserID"]." AND ID2 = ".$keys["ID"]."";

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

$data=db_fetch_array($rsExists);
if($data)
{

pop-up stating that this record already exists.

}

else

{

dal code from above

}
Thanks for any help,
Brian

C
cgphp 10/17/2012

In the server tab, enter the following code:



global $conn;

$strSQLExists = "SELECT ID, PID FROM Other_Table WHERE PID = ".$_SESSION["UserID"]." AND ID2 = ".$keys["ID"]."";

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

$data=db_fetch_array($rsExists);
if($data)

{

$result['msg'] = "Record already exists";

}

else

{

//dal code from above

$result['msg'] = "Record has been updated";

}


In the client after tab, enter the following code:

alert(result['msg']);
C
copper21 author 10/17/2012

That worked perfectly, thanks!
Just a couple more questions regarding buttons.

  1. Can I change the button to an image?
  2. Can I access any other data in that button from other tables? Could I use session variables or can I use $data or $values in the "Server" Tab? I would like to be able to access the id, of "Other_Table" so I can update it with another button on that list page.
  3. Can you hide a button I created like you can do with the add, edit, delete buttons?
    Thanks again,
    Brian

C
cgphp 10/17/2012
  1. The button is an anchor, so you can replace the link text with an image. Remove the span tags before the anchor.
  2. In the server tab you can access any type of data. $data and $values arrays are not available.

C
copper21 author 10/17/2012

Thanks Cristian!