I have a PHP form on page A.
I submit that form to page B, where the info goes into the database.
I also need to make an API call to add the submitted email address to MailChimp.
I'm using this mailchimp-api script: https://github.com/drewm/mailchimp-api
The form that enters the data works by itself, and the API that adds the email works by itself.
But when I try to do both on the same page, I get:
This page isn’t working
XYZ.com is currently unable to handle this request.
HTTP ERROR 500
The relevant code is below. I've tried switching the order of the calls (API and database). And I tried putting the API call inside the conditional statement:
if ($conn->query($sql) === TRUE) { }
Here is the whole code block:
<?php
// API call
include('MailChimp.php');
use \DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp('xxxxxxxxxxxxxxx-us1');
$list_id = 'xxxxxxxxxxx';
$email = $_POST['email'];
$result = $MailChimp->post("lists/$list_id/members", [
'email_address' => $email,
'status' => 'subscribed',
'interests' => ['xxxxxxxxxxx' => true],
]);
// database call
if(isset($_POST['addauth'])){
$email = $_POST['email'];
$conn = new mysqli("localhost", "xxxxxxxxxxx", "xxxxxxxxxxx", "xxxxxxxxxxx", 3306);
if ($conn->connect_errno) {
echo "Failed to connect to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
}
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$implanted = $_POST['implanted'];
$pacemaker = $_POST['pacemaker'];
$over_21 = $_POST['over_21'];
$updates = $_POST['updates'];
$sql = sprintf("INSERT INTO patients (FIRST_NAME, LAST_NAME, EMAIL, IMPLANTED, PACEMAKER, OVER_21, UPDATES, DATE) values ('%s','%s','%s','%s','%s','%s','%s', CURDATE())",
mysqli_real_escape_string($conn,$first_name),
mysqli_real_escape_string($conn,$last_name),
mysqli_real_escape_string($conn,$email),
mysqli_real_escape_string($conn,$implanted),
mysqli_real_escape_string($conn,$pacemaker),
mysqli_real_escape_string($conn,$over_21),
mysqli_real_escape_string($conn,$updates));
if ($conn->query($sql) === TRUE) {
//echo "New record created successfully" . $sql;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
} ?>