此查询应与Sendgrid一起用作Webhook,但注释掉的插入语句插入时,“ foreach”循环中的数据不会插入。我究竟做错了什么?您会注意到,我首先将数组默认设置为null ...
<?php $json = file_get_contents('php://input');
$obj = json_decode($json, true);
//error_log(print_r($obj, true));
$servername = "localhost";
$username = "******";
$password = "******";
$dbname = "******";
$defaults = array("email" => "null",
"timestamp" => "null",
"smtp-id" => "null",
"event" => "null",
"category" => "null",
"sg_event_id]" => "null",
"sg_message_id" => "null",
"response" => "null",
"attempt" => "null",
"useragent" => "null",
"ip" => "null",
"url" => "null",
"reason" => "null",
"status" => "null",
"asm_group_id" => "null" );
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
//$sql = "INSERT INTO sendgrid_response (email, timedate, smtp_id, event, category, sg_event_id, sg_message_id, response, attempt, useragent, ip, url, reason, status, asm_group_id )
// VALUES ('[email protected]', 12345678910, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)" ;
//$conn->query($sql);
foreach ($obj as $row) {
$row = array_merge($defaults, $row);
$sql = "INSERT INTO sendgrid_response (email, timedate, smtp-id, event, category, sg_event_id, sg_message_id, response, attempt, useragent, ip, url, reason, status, asm_group_id)
VALUES ('" . $row["email"] . "',
'" . $row["timestamp"] . "',
'" . $row["smtp_id"] . "',
'" . $row["event"] . "',
'" . $row["category"] . "',
'" . $row["sg_event_id"] . "',
'" . $row["sg_message_id"] . "',
'" . $row["response"] . "',
'" . $row["attempt"] . "',
'" . $row["useragent"] . "',
'" . $row["ip"] . "',
'" . $row["url"] . "',
'" . $row["reason"] . "',
'" . $row["status"] . "',
'" . $row["asm_group_id"] . "')";
$conn->query($sql); }
?>
[我发现了,在“ smtp-id”中有一个“-”,我在代码,数据库字段和中提琴中将其更改为下划线!