您好,当我想比较从 esp8266 发送的值是否与 php 中的值相同或仅检查此值是否已设置时,它总是返回 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();
1 回答
杨魅力
TA贡献1811条经验 获得超6个赞
这是因为&
这里之前的空间:
key=1A2B3C4D &pressed=
这是设置$_POST['key']
为"1A2B3C4D "
,不等于"1A2B3C4D"
。比较字符串时,空格很重要。将其更改为
key=1A2B3C4D&pressed=
如果要忽略字符串周围的空格,请使用该trim()
函数。
if(trim($_POST['key']) == "1A2B3C4D"){
- 1 回答
- 0 关注
- 129 浏览
添加回答
举报
0/150
提交
取消