尝试使用 POST 将数据添加到我的 MySQL 数据库

Trying to add data to my MySQL database using POST

我正在尝试在我的服务器上使用 POST 方法并在我的 ESP8266 V3 板上使用 client.print 方法将数据添加到我的 MySQL 数据库。

这是我的 Arduino 代码,它似乎可以正常工作,因为没有发生错误并且串行监视器会完成所有步骤。

#include <ESP8266WiFi.h>

const char* ssid     = "FRITZ!Box 7430 BZ";
const char* password = "*************";

const char* host = "alexander-productions.de/mysql";
const char* streamId   = "....................";
const char* privateKey = "....................";

void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
     would try to act as both a client and an access-point and could cause
     network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

int value = 0;

void loop() {
  delay(5000);
  ++value;

  Serial.print("connecting to ");
  Serial.println(host);

  ///
  String a = "TestID";
  String b = "TestNachmame";
  String data = "value1=" + a + "&value2=" + b;
  ///

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  } else {
    Serial.println("connection successful");
    client.println("POST /post.php HTTP/1.1");
    client.println("Host: alexander-productions.de/mysql"); // SERVER ADDRESS HERE TOO
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println();
    client.print(data);
    Serial.println("Sending to Database successful!");
  }


  Serial.println();
  Serial.println("closing connection");
}

这是我的服务器代码,应该将数据添加到 MySQL 数据库。

<?php
$servername = "**********";
$username = "*****";
$password = "*****";
$dbname = "******";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
echo "<br/>";

$var1=$_POST["value1"];
$var2=$_POST["value2"];

echo $var1;
echo "<br/>";
echo $var2;
echo "<br/>";

$query = "INSERT INTO `accounts` (`firstName`, `lastName`)
VALUES ('".$var1."','".$var2."')";

if (mysqli_query($conn, $query)) {
    echo "New record created successfully" . "<br />";
} else {
    echo "Error: " . $query . "<br>" . mysqli_error($conn);
}
#mysql_query($query,$conn);
mysqli_close($conn);
?>

问题是我的数据库中没有任何新条目 table 而且我不知道如何调试,因为我看不到 Arduino 尝试调试时服务器端发生了什么POST数据。

当在浏览器上打开 post.php 文件时它起作用了,因为数据库获得了一个包含 NULL 的新条目。

alexander-productions.de/mysql 不是有效的主机。
将其更改为 alexander-productions.de.

然后将文件夹添加到请求路径,即 /mysql/post.php

它应该看起来像:

const char* host = "alexander-productions.de";
client.println("POST /mysql/post.php HTTP/1.1");
client.print("Host: ");
client.println(host); // non hardcoded host header
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);