iT邦幫忙

0

xampp連接php傳資料至heidisql連不上

小弟最近電腦重灌所以一些程式都重新安裝
我在使用wemos及xampp連接php將資料傳至資料庫時出現了問題
但我換了兩台不一樣的電腦用一樣的流程進行操作都是成功的
我用了以下流程進行了連接
1.開啟apache及mysql
https://ithelp.ithome.com.tw/upload/images/20210605/201363304mxJaKZXSj.png
2.使用的帳號
https://ithelp.ithome.com.tw/upload/images/20210605/20136330O3nfhWZujO.png
3.arduino端的程式
https://ithelp.ithome.com.tw/upload/images/20210605/20136330rLCipqdlGF.png

//Wi-Fi
#include "ESP8266WiFi.h"
#include "ESP8266WiFiMulti.h"
ESP8266WiFiMulti WiFiMulti;
#define wifi_id "1916250"
#define wifi_pass "22303847"



char type;
float ml;
float d;


void setup() {
  Serial.begin(9600);//設定鮑率115200
  
  WiFiMulti.addAP(wifi_id, wifi_pass);//添加AP(WiFi名稱 ,WiFi密碼)

  //如果連線成功會回傳 WL_CONNECTED,故以此確認連線狀態
  while(WiFiMulti.run() != WL_CONNECTED){
    Serial.println("wait..");
    delay(500);  
  }
}

void loop() {
    
  const uint16_t port =8787;//資料庫的 PORT
  const char * host ="192.168.33.10"; //server 的IP
  Serial.print("連線至[");
  Serial.print(host);
  Serial.println("]");

  WiFiClient client; // 建立客戶端

  
  //檢查是否與WiFi連線
  if(!client.connect(host, port)){
      Serial.println("connect failed");
      Serial.println("wait 3 sec...");
      delay(3000);
      return;//   return在程序中有退出loop函數的作用,但是loop函數會再次運行起來      
  }

  
  char type[]="洗手模式";
  ml=447;
  d=25;
  
  //連線至伺服端後 傳送資料至getData.php
  String url ="/aiot/addData2.php?type="+String(type)+"&ml="+String(ml)+"&d="+String(d);   //關鍵!!
  client.print(String("GET ")+url+" HTTP/1.1\r\n"+
  "Host: "+host+"\r\n"+
  "Connection: close\r\n\r\n");

  Serial.println(type);//測試
  Serial.println("connect closed");
  client.stop();
  delay(5000);//延時5秒
}

4.php檔放置的路徑
https://ithelp.ithome.com.tw/upload/images/20210605/20136330ReJJBd0fAm.png

php檔的內容

<?php 

$type=$_GET[type];
$ml=$_GET[ml];
$d=$_GET[d];

//$h=0;
//$t=0;


//connect db////////////////////
$mysqli = new mysqli("localhost","test1234","test1234","aiotdb");

// Check connection
if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}

// Perform query//////////////

//$mysqli -> query("INSERT INTO waterml (ml,durationtime) VALUES (100,100)");
//$mysqli -> query("INSERT INTO waterml (ml,durationtime) VALUES (".$ml.",".$d.")");
//$mysqli -> query("INSERT INTO waterml (type,ml,durationtime) VALUES ('洗手模式',".$ml.",".$d.")");

$mysqli -> query("INSERT INTO waterml (type,ml,durationtime) VALUES ('$type',".$ml.",".$d.")"); 

$mysqli -> close();






 ?>

5.heidisql資料表單
https://ithelp.ithome.com.tw/upload/images/20210605/20136330d3jUyf5xXd.png

自己有去檢查是否是表單還是帳號的問題,最後換了電腦測試才成功
希望有高手能幫我找出問題

後來我分別在不同電腦上用瀏覽器連進http://localhost/aiot/addData2.php 這個網址
我在其他電腦上是能連進去的
以下是正常的畫面
https://ithelp.ithome.com.tw/upload/images/20210607/20136330WOytIPzYPU.png

但在我的電腦會跳出拒絕連線
https://ithelp.ithome.com.tw/upload/images/20210607/20136330fxbkyIE0Qt.png

haward79 iT邦研究生 2 級 ‧ 2021-06-06 15:18:36 檢舉
(1) heidisql 是頂層軟體,你的底層是 MySQL。
(2) 程式碼請直接貼,截圖不是個好方法。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
小魚
iT邦大師 1 級 ‧ 2021-06-05 09:11:55

傳資料到HeidiSQL是甚麼...
應該是寫到MySql裡面吧...

我要發表回答

立即登入回答