gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 17:24:15 24 4
gpt4 key购买 nike

我正在尝试使用服务器上的 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 不是有效主机。
将其更改为 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);

关于c++ - 尝试使用 POST 将数据添加到我的 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49540852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com