dragoninasia2014 2018-07-25 12:24
浏览 44

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

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

} ?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
    • ¥15 gdf格式的脑电数据如何处理matlab
    • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
    • ¥100 监控抖音用户作品更新可以微信公众号提醒
    • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
    • ¥70 2048小游戏毕设项目
    • ¥20 mysql架构,按照姓名分表
    • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
    • ¥15 linux驱动,linux应用,多线程