API和数据库调用在同一页面上

时间:2018-07-25 12:24:40

标签: php mysql mailchimp-api-v3.0

我在A页上有一个PHP表单。

我将该表单提交到页面B,信息将进入数据库。

我还需要进行API调用,以将提交的电子邮件地址添加到MailChimp。

我正在使用以下mailchimp-api脚本:https://github.com/drewm/mailchimp-api

输入数据的表单本身起作用,添加电子邮件的API本身起作用。

但是当我尝试在同一页面上同时执行这两项操作时,我得到:

This page isn’t working
XYZ.com is currently unable to handle this request.
HTTP ERROR 500

相关代码如下。我尝试过切换调用的顺序(API和数据库)。我尝试将API调用放入条件语句中

if ($conn->query($sql) === TRUE) { }

这是整个代码块:

    <?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();

} ?>

0 个答案:

没有答案