Web Analytics Made Easy -
StatCounter echo json_encode will not echo after I do a mysql db write - CodingForum

Announcement

Collapse
No announcement yet.

echo json_encode will not echo after I do a mysql db write

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • echo json_encode will not echo after I do a mysql db write

    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('[email protected]', '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?

  • #2
    The posted code is missing a closing }, which results in a php syntax error and none of the code runs.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

    Comment

    Working...
    X