Json_encode echo goes away when data is stored in mysql table, why?

I am working on a form processor that works normally but decided to customize it by adding in a MYSQL database connect based on the successful connection, but it doesn’t echo the result on the form page like it did`. When I remove the mysql db code it works again. How can I prevent this issue from happening. The transaction happens at Authorize.net sandbox and the phpmyadmin is showing data is being written to table but I’d like to keep it echoing when it have a successful transaction.

Code:

if ($response != null)
{
    $tresponse = $response->getTransactionResponse();

    error_log(json_encode($tresponse));

    if (($tresponse != null) && ($tresponse->getResponseCode()=="1") )   
    {

        if ( $tresponse->getTransId() ) {

            $emailBody = generateEmailTransaction($tresponse->getTransId(), $invoiceNumber);

           
            if ( ! file_exists('transactions') ) {
                mkdir('transactions');
                file_put_contents(__DIR__ . '/transactions/index.html', "<!-- silence is golden -->");
            }

            file_put_contents(__DIR__ . '/transactions/' . $invoiceNumber . '.txt', $emailBody);

            //to admin
            mail('me@myyemail.com', 'Dinner Confirmation Receipt', $emailBody);

            //to user
            mail($_POST['email'], 'Dinner Confirmation Receipt', $emailBody);

            echo json_encode(
                array(
                    'success' => true, 
                    'transaction_id' => $tresponse->getTransId()
                )
            );
           
    
            // a string 
           $ad_choice = generateAdChoice($tresponse->getTransId(),$invoiceNumber); 
             
            
        }
         
        //Write to MYSQL DB /////////
           $servername = "whatever.com";
            $username = "paypage";
            $password = "pass";
            $dbname = "mydatabse";

            // Create connection
            $conn = mysqli_connect($servername, $username, $password, $dbname);
            // Check connection
            if (!$conn) {
                exit;
                die("Connection failed: " . mysqli_connect_error());
            }
            //if ad_choice
            if ( ! empty($ad_choice2) ) {
                
                $sql = "INSERT INTO paylist (bought, omit)
                VALUES ('$ad_choice', '')";
                exit;
                if (mysqli_query($conn, $sql)) {
                    //echo "New record created successfully";
                } else {
                    //echo "Error: " . $sql . "<br>" . mysqli_error($conn);
                }
            }//if ad_choice ends

            mysqli_close($conn);    

            ///////MYSQL WRITE END/////// 
        
        
        
    }

What is it that causes echo json_encode to now not echo any more after the “write to database” section is in there?

I stab a guess at the exit. and in the db section you should not echo anything at all, no matter what happens.

seems like that did not matter but I will try and get this down to bare minimum with no echo and let you know.

What looks off to me are the exit lines.
Exit means just that - exit as in leave
So anything after those lines will never happen.

            if (!$conn) {
                exit;
                die("Connection failed: " . mysqli_connect_error());
            }
            //if ad_choice
            if ( ! empty($ad_choice2) ) {
                
                $sql = "INSERT INTO paylist (bought, omit)
                VALUES ('$ad_choice', '')";
                exit;
                if (mysqli_query($conn, $sql)) {
                    //echo "New record created successfully";
                } else {
                    //echo "Error: " . $sql . "<br>" . mysqli_error($conn);
                }

I removed the exits and everything I thought was output. I even tried ob_end_clean(); and I thought it then failed to pass data to the database. Here is the function code for creating the $ad_choice variable

[code]function generateAdChoice($transactionId, $invoiceNumber) {

////////////////////////////////////
//BUILD ADCHOICE CONTENT //
////////////////////////////////////

$ad = array(
    "ad_choice" => "Donation:",
);

foreach ($ad as $key => $value) {
    if ( ! empty($_POST[$key]) ) {

        if (is_array($_POST[$key])){

                foreach ($_POST[$key] as $value) {
                        $ad_choice .= $value . ', ';
                }
                
        } else {
        $ad_choice .= $value . "\n";
        }
    }
}    

 return $ad_choice;

}[/code]

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.