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

时间:2018-03-28 17:35:44

标签: c++ http arduino arduino-esp8266

我尝试使用我服务器上的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);
?>

问题是我没有在我的数据库表中获得任何新条目而且我不知道如何调试,因为当Arduino尝试时我无法看到服务器端发生的情况POST数据。

在浏览器上打开post.php文件时,由于数据库获取包含NULL的新条目,因此可以正常工作。

1 个答案:

答案 0 :(得分:0)

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