api和数据库调用在同一页上

fhg3lkii  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(155)

我在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();

} ?>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题