【问题标题】:Trying to add data to my MySQL database using POST尝试使用 POST 将数据添加到我的 MySQL 数据库
【发布时间】:2018-03-28 17:35:44
【问题描述】:

我正在尝试使用我的服务器上的 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 的新条目。

【问题讨论】:

  • alexander-productions.de/mysql 不是有效的主机。
  • @gre_gor 哦,如果我的文件位于文件夹中,我该怎么办?
  • 更改/post.php

标签: c++ http arduino arduino-esp8266


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-12
    • 2013-10-18
    • 2021-02-18
    • 2017-07-14
    • 2017-01-15
    • 2016-05-07
    • 2017-04-29
    相关资源
    最近更新 更多