This topic is locked
[SOLVED]

Scratching My Head - Button Activating Without Being Pressed

8/8/2021 7:32:13 PM
PHPRunner General questions
D
DRCR Dev author

I have the following code in a button. Problem is the button is activating when not pressed. Everytime something on the page changes, this button reactivates. I cannot work out what I've done wrong.

This is in the server section of the button. Any advice will be apprecated. I've exhausted my brain juice on this one.


$rs = DB::Query("select ID, photo_name, approved from private_photos where (ID='".$_SESSION["UserID"]."' AND photo_name ='1' AND approved='1')");
$data=$rs->fetchAssoc();
if($data)
{
$data = array();
$data["ID"] = $_SESSION["UserID"];
$data["approved"] = "1";
$data["photo_name"] = "1";
$rs = DB::Select("private_photos", $data );
while( $record1 = $rs->fetchAssoc() )
{
$_SESSION["private_photo1"] = $record1["photo"];
}
$data = array();
$data["private_temp"] = $_SESSION["private_photo1"];
$data["ownerID"] = $_SESSION["UserID"];
$data["targetID"] = $_SESSION["targetid"];
$data["messages"] = "My Private Pics";
DB::Insert("chat_history",$data);
}
else
{

}
$rs = DB::Query("select ID, photo_name, approved from private_photos where (ID='".$_SESSION["UserID"]."' AND photo_name ='2' AND approved='1')");
$data=$rs->fetchAssoc();
if($data)
{
$data = array();
$data["ID"] = $_SESSION["UserID"];
$data["approved"] = "1";
$data["photo_name"] = "2";
$rs = DB::Select("private_photos", $data );
while( $record2 = $rs->fetchAssoc() )
{
$_SESSION["private_photo2"] = $record2["photo"];
}
$data = array();
$data["private_temp"] = $_SESSION["private_photo2"];
$data["ownerID"] = $_SESSION["UserID"];
$data["targetID"] = $_SESSION["targetid"];
$data["messages"] = "My Private Pics";
DB::Insert("chat_history",$data);
}
else
{

}
$rs = DB::Query("select ID, photo_name, approved from private_photos where (ID='".$_SESSION["UserID"]."' AND photo_name ='3' AND approved='1')");
$data=$rs->fetchAssoc();
if($data)
{
$data = array();
$data["ID"] = $_SESSION["UserID"];
$data["approved"] = "1";
$data["photo_name"] = "3";
$rs = DB::Select("private_photos", $data );
while( $record3 = $rs->fetchAssoc() )
{
$_SESSION["private_photo3"] = $record3["photo"];
}
$data = array();
$data["private_temp"] = $_SESSION["private_photo3"];
$data["ownerID"] = $_SESSION["UserID"];
$data["targetID"] = $_SESSION["targetid"];
$data["messages"] = "My Private Pics";
DB::Insert("chat_history",$data);
}
else
{

}
$rs = DB::Query("select ID, photo_name, approved from private_photos where (ID='".$_SESSION["UserID"]."' AND photo_name ='4' AND approved='1')");
$data=$rs->fetchAssoc();
if($data)
{
$data = array();
$data["ID"] = $_SESSION["UserID"];
$data["approved"] = "1";
$data["photo_name"] = "4";
$rs = DB::Select("private_photos", $data );
while( $record4 = $rs->fetchAssoc() )
{
$_SESSION["private_photo4"] = $record4["photo"];
}
$data = array();
$data["private_temp"] = $_SESSION["private_photo4"];
$data["ownerID"] = $_SESSION["UserID"];
$data["targetID"] = $_SESSION["targetid"];
$data["messages"] = "My Private Pics";
DB::Insert("chat_history",$data);
}
else
{

}
$rs = DB::Query("select ID, photo_name, approved from private_photos where (ID='".$_SESSION["UserID"]."' AND photo_name ='5' AND approved='1')");
$data=$rs->fetchAssoc();
if($data)
{
$data = array();
$data["ID"] = $_SESSION["UserID"];
$data["approved"] = "1";
$data["photo_name"] = "5";
$rs = DB::Select("private_photos", $data );
while( $record5 = $rs->fetchAssoc() )
{
$_SESSION["private_photo5"] = $record5["photo"];
}
$data = array();
$data["private_temp"] = $_SESSION["private_photo5"];
$data["ownerID"] = $_SESSION["UserID"];
$data["targetID"] = $_SESSION["targetid"];
$data["messages"] = "My Private Pics";
DB::Insert("chat_history",$data);
}
else
{

}
$rs = DB::Query("select ID, photo_name, approved from private_photos where (ID='".$_SESSION["UserID"]."' AND photo_name ='6' AND approved='1')");
$data=$rs->fetchAssoc();
if($data)
{
$data = array();
$data["ID"] = $_SESSION["UserID"];
$data["approved"] = "1";
$data["photo_name"] = "6";
$rs = DB::Select("private_photos", $data );
while( $record6 = $rs->fetchAssoc() )
{
$_SESSION["private_photo6"] = $record6["photo"];
}
$data = array();
$data["private_temp"] = $_SESSION["private_photo6"];
$data["ownerID"] = $_SESSION["UserID"];
$data["targetID"] = $_SESSION["targetid"];
$data["messages"] = "My Private Pics";
DB::Insert("chat_history",$data);
}
else
{

}
$rs = DB::Query("select ID, photo_name, approved from private_photos where (ID='".$_SESSION["UserID"]."' AND photo_name ='7' AND approved='1')");
$data=$rs->fetchAssoc();
if($data)
{
$data = array();
$data["ID"] = $_SESSION["UserID"];
$data["approved"] = "1";
$data["photo_name"] = "7";
$rs = DB::Select("private_photos", $data );
while( $record7 = $rs->fetchAssoc() )
{
$_SESSION["private_photo7"] = $record7["photo"];
}
$data = array();
$data["private_temp"] = $_SESSION["private_photo7"];
$data["ownerID"] = $_SESSION["UserID"];
$data["targetID"] = $_SESSION["targetid"];
$data["messages"] = "My Private Pics";
DB::Insert("chat_history",$data);
}
else
{

}

$rs = DB::Query("select ID, photo_name, approved from private_photos where (ID='".$_SESSION["UserID"]."' AND photo_name ='8' AND approved='1')");
$data=$rs->fetchAssoc();
if($data)
{
$data = array();
$data["ID"] = $_SESSION["UserID"];
$data["approved"] = "1";
$data["photo_name"] = "8";
$rs = DB::Select("private_photos", $data );
while( $record8 = $rs->fetchAssoc() )
{
$_SESSION["private_photo8"] = $record8["photo"];
}
$data = array();
$data["private_temp"] = $_SESSION["private_photo8"];
$data["ownerID"] = $_SESSION["UserID"];
$data["targetID"] = $_SESSION["targetid"];
$data["messages"] = "My Private Pics";
DB::Insert("chat_history",$data);
}
else
{

}
$rs = DB::Query("select ID, photo_name, approved from private_photos where (ID='".$_SESSION["UserID"]."' AND photo_name ='9' AND approved='1')");
$data=$rs->fetchAssoc();
if($data)
{
$data = array();
$data["ID"] = $_SESSION["UserID"];
$data["approved"] = "1";
$data["photo_name"] = "9";
$rs = DB::Select("private_photos", $data );
while( $record9 = $rs->fetchAssoc() )
{
$_SESSION["private_photo9"] = $record9["photo"];
}
$data = array();
$data["private_temp"] = $_SESSION["private_photo9"];
$data["ownerID"] = $_SESSION["UserID"];
$data["targetID"] = $_SESSION["targetid"];
$data["messages"] = "My Private Pics";
DB::Insert("chat_history",$data);
}
else
{

}
$rs = DB::Query("select ID, photo_name, approved from private_photos where (ID='".$_SESSION["UserID"]."' AND photo_name ='10' AND approved='1')");
$data=$rs->fetchAssoc();
if($data)
{
$data = array();
$data["ID"] = $_SESSION["UserID"];
$data["approved"] = "1";
$data["photo_name"] = "10";
$rs = DB::Select("private_photos", $data );
while( $record10 = $rs->fetchAssoc() )
{
$_SESSION["private_photo10"] = $record10["photo"];
}
$data = array();
$data["private_temp"] = $_SESSION["private_photo10"];
$data["ownerID"] = $_SESSION["UserID"];
$data["targetID"] = $_SESSION["targetid"];
$data["messages"] = "My Private Pics";
DB::Insert("chat_history",$data);
}
else
{

}
A
acpan 8/9/2021

Here's a quick comment:

  1. Be careful when you use $data to store values, $data array is used in some events by PHPRunner. Eg:
    Before/After Record Process Events, on list page custom codes. You may overwrite the $data values, which may give you unpredictable result. (Always check the parameters help on top of your codes, if the $data array is used by PHPRunner)


  2. What do you mean by something on page change? You click something or you refresh the page? Your definition of "button reactivates", means your SQL statement executed, OR the values displayed on screen are changing ?
    If it affects the values display, then it could be due to (1) above.


  3. You should try to optimise your SQL statement in such a case, eg, using INSERT INTO SELECT, it will run 1 request to the DB server. It also makes it easier to debug. Here's how you could do it:



// Save the session variables first to local variables for ease of forming and
// debugging your SQL statement:

$targetid = $_SESSION["targetid"];
$userid = $_SESSION["UserID"];

// form the DB Insert fields for **chat_history table**
$fields = "private_temp, ownerID, targetID, messages";

// Get all the rows that meet the condition from **private_photos** table
// Tips: Replace $targetid to actual target id and run the Select statement below manually
// from PHPMyAdmin and see if the return list is expected.

$value_list = "SELECT photo, ID, '$targetid', 'My Private Pics' from private_photos
WHERE ID='$userid' AND approved='1' AND
(photo_name ='1' OR photo_name ='2' OR photo_name ='3' OR photo_name ='4' OR
photo_name ='5' OR photo_name ='6' OR photo_name ='7' OR photo_name ='8' OR photo_name ='9'
OR photo_name ='10') ";

// Insert all rows at once in one statement
$sql = "INSERT INTO chat_history ( $fields) VALUES $value_list ";
DB::Exec($sql);
if ( DB::LastError() ) {
// console.log(result["error"]) at Client After event, if need to.
$result["error"] = "SQL=$sql Error:". DB::LastError();
}

More info about INSERT INTO SELECT

D
DRCR Dev author 8/9/2021

Thanks. I think I've been pointed in the right direction. I have $data in a few buttons on the page and never had any issues before adding more buttons.

I meant the button was executing without being pressed. It's on the chat script where whenever I type a message, the message is sent and the button executes. I think sending a message may trigger a page refresh, but I haven't looked carefully at what happens on refresh. I'm just guessing there.

Thanks for the help on the query. I was sure there had to be a smarter way to write it but this makes a lot more sense.

-Cec

A
acpan 8/9/2021

I think tthere could be some JS codes that trigger the "buttton pressed" programmatically, at fixed interval or page refresh, or when a message is sent.

Try to check javascript on load event, custom_functions.js or external js script that you included in your header.php.

D
DRCR Dev author 8/10/2021

The issue was definitely in the original code. When I removed it, all the other buttons and the chat behave as expected.

I'm struggling to format the syntax on the new expression. I've tested in PHPmyadmin and the select statement is valid. I cant work out why the mapping of the fields does not work

$targetid = $_SESSION["targetid"];
$userid = $_SESSION["UserID"];

$fields = "ownerid, private_temp, targetid, messages";

$values = "SELECT ID, photo FROM private_photos
WHERE ID='$userid' AND approved='1' AND type='1'";

// ownerid = ID / private_temp = photo / targetid = $targetid / messages = 'Private Pics'
$value_string = "$values, $targetid, 'Private Pics'";

$sql = "INSERT INTO chat_history ( $fields) VALUES $value_string ";
DB::Exec($sql);

if ( DB::LastError() ) {
// console.log(result["error"]) at Client After event, if need to.
$result["error"] = "SQL=$sql Error:". DB::LastError();
}

This is the mapping table from one table (private_pictures) to chat_history
// ownerid = ID / private_temp = photo / targetid = $targetid / messages = 'Private Pics'
targetid and messages are not in the original select statement and I need them to be $targetid and simply the text "Private Pics"

I never received an error when clicking the button. It has to syntax.

A
acpan 8/10/2021

Try replace:

$values = "SELECT ID, photo FROM private_photos
WHERE ID='$userid' AND approved='1' AND type='1'";

// ownerid = ID / private_temp = photo / targetid = $targetid / messages = 'Private Pics'
$value_string = "$values, $targetid, 'Private Pics' ";

with

$value_string = "SELECT ID, photo, $targetid, 'Private Pics' FROM private_photos
WHERE ID='$userid' AND approved='1' AND type='1' ";


Note: The fields selected must match the insert fields, same as my above example.
Also if $targetid is a string, you must enquote it in your select statement.

D
DRCR Dev author 8/10/2021

Thanks for your help. I got this to work and I understand how to use the use these tricks to simplify the code.

$targetid = $_SESSION["targetid"];
$userid = $_SESSION["UserID"];

$fields = "ownerid, private_temp, targetid, messages";

$value_string = "SELECT ID, photo, $targetid AS something2, 'Private Pics' AS something FROM private_photos
WHERE ID='$userid' AND approved='1' AND type='1'";

$sql = "INSERT INTO chat_history ($fields) $value_string";
DB::Exec($sql);