为什么发布到 php 的数据可以打印但不能在 if 中进行比较。 (esp8266 only.PC 网络浏览器正常工作)
Why posted data to php can be printed but not compared in if. (esp8266 only.PC web browser works correctly)
你好,当我想比较从 esp8266 发送的值是否与 php 中的值相同或者只检查是否设置了这个值时,它总是 return false
我试着打印回这些数据,虽然 isset 说它没有设置,但它有效。但是当我用电脑尝试这个时,我的代码正常
if(isset($_POST['key'])){
if($_POST['key'] == "1A2B3C4D"){
$POWER = "";
if($_POST['pressed'] == "1"){
$sql = "SELECT `POWER` FROM `VolleyTest` WHERE ID = 1";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_row($result)){
$POWER = $row[0];
}
if($POWER == "OFF"){
$POWER = "ON";
}else{
$POWER = "OFF";
}
$sql = 'UPDATE `VolleyTest` SET `POWER`="'.$POWER.'" WHERE ID = 1';
$conn->query($sql);
}
$sql = "SELECT `POWER` FROM `VolleyTest` WHERE ID = 1";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_row($result)){
$POWER = $row[0];
}
echo $POWER;//. " ". $_POST['pressed'] . " " . $_POST['key'];
}
}
esp8266:
btn.Check();
String state = "0";
if(btn.Get()){
state = "1";
}
HTTPClient http; //Declare object of class HTTPClient
btn.Check();
String postData = "key=1A2B3C4D &pressed=" + state;//Post Data
Serial.println(postData);
btn.Check();
http.begin("http://"+host+":38180/read_from_nodemcu.php");//Specify request destination
btn.Check();
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
btn.Check();
int httpCode = http.POST(postData); //Send the request
btn.Check();
String payload = http.getString(); //Get the response payload
btn.Check();
http.end(); //Close connection
btn.Check();
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
btn.Check();
if(httpCode == 200){
if(payload == "ON"){
digitalWrite(D0,HIGH);
}else if(payload == "OFF"){
digitalWrite(D0,LOW);
}else {
Serial.println("Error");
}
}
btn.Check();
因为这里&
之前的space:
key=1A2B3C4D &pressed=
这是将 $_POST['key']
设置为 "1A2B3C4D "
,这不等于 "1A2B3C4D"
。比较字符串时空格很重要。改成
key=1A2B3C4D&pressed=
如果要忽略字符串周围的 space,请使用 trim()
函数。
if(trim($_POST['key']) == "1A2B3C4D"){
你好,当我想比较从 esp8266 发送的值是否与 php 中的值相同或者只检查是否设置了这个值时,它总是 return false
我试着打印回这些数据,虽然 isset 说它没有设置,但它有效。但是当我用电脑尝试这个时,我的代码正常
if(isset($_POST['key'])){
if($_POST['key'] == "1A2B3C4D"){
$POWER = "";
if($_POST['pressed'] == "1"){
$sql = "SELECT `POWER` FROM `VolleyTest` WHERE ID = 1";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_row($result)){
$POWER = $row[0];
}
if($POWER == "OFF"){
$POWER = "ON";
}else{
$POWER = "OFF";
}
$sql = 'UPDATE `VolleyTest` SET `POWER`="'.$POWER.'" WHERE ID = 1';
$conn->query($sql);
}
$sql = "SELECT `POWER` FROM `VolleyTest` WHERE ID = 1";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_row($result)){
$POWER = $row[0];
}
echo $POWER;//. " ". $_POST['pressed'] . " " . $_POST['key'];
}
}
esp8266:
btn.Check();
String state = "0";
if(btn.Get()){
state = "1";
}
HTTPClient http; //Declare object of class HTTPClient
btn.Check();
String postData = "key=1A2B3C4D &pressed=" + state;//Post Data
Serial.println(postData);
btn.Check();
http.begin("http://"+host+":38180/read_from_nodemcu.php");//Specify request destination
btn.Check();
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
btn.Check();
int httpCode = http.POST(postData); //Send the request
btn.Check();
String payload = http.getString(); //Get the response payload
btn.Check();
http.end(); //Close connection
btn.Check();
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
btn.Check();
if(httpCode == 200){
if(payload == "ON"){
digitalWrite(D0,HIGH);
}else if(payload == "OFF"){
digitalWrite(D0,LOW);
}else {
Serial.println("Error");
}
}
btn.Check();
因为这里&
之前的space:
key=1A2B3C4D &pressed=
这是将 $_POST['key']
设置为 "1A2B3C4D "
,这不等于 "1A2B3C4D"
。比较字符串时空格很重要。改成
key=1A2B3C4D&pressed=
如果要忽略字符串周围的 space,请使用 trim()
函数。
if(trim($_POST['key']) == "1A2B3C4D"){