1

I am building an API in PHP for a desktop program I am building. I have created a nested update if select statement as provided below; I am trying to respond if 1 row was updated or 0 were updated (likely due to my nested statement)

function Savetimes($RID, $uid, $curtoken, $PSAP, $DispatchNotified, $Dispatched, $Confirm, $Enroute, $Onscene, $ArrivedPt, $TransferPt, $Transporting, $ArriveDestination, $TransferCare, $Cleared, $Canceled) 
    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(PDOException $e) {
        //  echo "Connection failed: " . $e->getMessage();
    }

    $statement = $conn->prepare("UPDATE `Appointments` 
                SET `PSAP` = :PSAP, 
                    `DispatchNotified` = :DispatchNotified, 
                    `Dispatched` = :Dispatched, 
                    `Confirm` = :Confirm, 
                    `Enroute` = :Enroute, 
                    `Onscene` = :Onscene, 
                    `ArrivedPt` = :ArrivedPt, 
                    `TransferPt` = :TransferPt, 
                    `Transporting` = :Transporting, 
                    `ArriveDestination` = :ArriveDestination, 
                    `TransferCare` = :TransferCare, 
                    `Cleared` = :Cleared, 
                    `Canceled` = :Canceled 
                WHERE `RID` = :RID 
                and (
                        (
                            SELECT count(*) 
                            From `Token` 
                            WHERE ukey = :ukey 
                            AND uid = :uid 
                            AND TimeExp >= now() 
                            AND TTL > 0
                        ) > 0)
                ");

    $statement->execute(array(
        "PSAP" => $PSAP,
        "DispatchNotified" => $DispatchNotified,    
        "Dispatched" => $Dispatched,
        "Confirm" => $Confirm,
        "Enroute" => $Enroute,
        "Onscene" => $Onscene,
        "ArrivedPt" => $ArrivedPt,
        "TransferPt" => $TransferPt,
        "Transporting" => $Transporting,
        "ArriveDestination" => $ArriveDestination,
        "TransferCare" => $TransferCare,
        "Cleared" => $Cleared,
        "Canceled" => $Canceled,
        "RID" => $RID,
        'ukey' => $curtoken,
        'uid' => $uid
        ));
    $UpdateCount = $statement->rowCounts();

    return $UpdateCount;
}

to further explain the update statement: The Appointment will be updated with data provided through the API (I simplified the code to remove about 100 lines of data processing before the update statement). It will Only update if the nested Select statement searching for an unexpired token match counts more than 0 rows.

My process is failing to count the updated rows - any assistance is appreciated.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
James Stafford
  • 1,034
  • 1
  • 15
  • 41
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly May 07 '19 at 17:33
  • my code is well indented however much of it failed to carry when I copied and pasted and then even more was demolished when I removed sensitive data and data processing; however I am well aware of the length of the select statement leaves much to be desired. – James Stafford May 07 '19 at 17:35
  • Your problem is the lack of error reporting. PHP would promptly tell you what is the error but you never asked. Please check my [PHP error reporting](https://phpdelusions.net/articles/error_reporting) article – Your Common Sense May 07 '19 at 18:03
  • while ultimately enabling logging and pulling my log gave me the result about 20 minutes prior to your reply - your response indicated a failure to read the code; Error reporting was enabled but again being that this is operating as an API I was not easily able to trigger the error from a browser. – James Stafford May 07 '19 at 18:19
  • I was talking of PHP error reporting which is not to be seen in the code above. Like it is said in the linked question, in order to see PDO errors, you have to be able to see PHP errors in general. This is what I am taking about. – Your Common Sense May 07 '19 at 19:15

0 Answers0