2016-10-17 5 views
0

에 아두 이노를 사용하고 esp8266 무선 랜 모듈을 사용하여 SQL 데이터베이스에 데이터를 보내려고하지만, 내가 통지를 받고 있어요PHP 정의되지 않은 인덱스 나는 현재 PHP와 MySQL 모두 합리적으로 새로운 해요 MySQL의

+ IPD, 0,348은 :
공지 : 정의되지 않은 인덱스 : C에서 lic_no : \ XAMPP \ htdocs를 \ test1.php onle < 2 \ aphdstt.h /> NLN <> < bNie/: DFN NX otoib : XP \ 직류 \ s1hr/ueuy0LSD

그러나 여전히 모든 컬럼에서 내 SQL 데이터베이스에 null 값을 얻고 있습니다.

나는이 포럼에서 제안 된 많은 것들을 운없이 시험해 보았습니다.

PHP 코드 :

<?php 
$dbhost = 'localhost'; 
$dbuser = 'Ecte350'; 
$dbpass = ''; 
$conn = mysql_connect($dbhost, $dbuser, $dbpass); 

$lic_no = isset($_POST['lic_no'])? $_POST['lic_no']: false; 
$speed = isset($_POST['speed'])? $_POST['speed']: false; 
$location = isset($_POST['location'])? $_POST['location']: false; 

    if(! $conn) { 
     die('Could not connect: ' . mysql_error()); 
    } 

    $sql = 'INSERT INTO data '. 
     '(lic_no, speed, location) '. 'VALUES ("'.$_POST["lic_no"].'", "'.$_POST["speed"].'", "'.$_POST["location"].'")'; 

    mysql_select_db('ecte350'); 
    $retval = mysql_query($sql, $conn); 

    if(! $retval) { 
     die('Could not enter data: ' . mysql_error()); 
    } 

    echo "Entered data successfully\n"; 

    mysql_close($conn); 
?> 

을 그리고 이것은 내 아두 이노 코드입니다 :

#include <WiFi.h> 
#include <WiFiClient.h> 
#include <WiFiServer.h> 
#include <WiFiUdp.h> 



////////////////////// 
// Library Includes // 
////////////////////// 
// SoftwareSerial is required (even you don't intend on 
// using it). 
#include <SoftwareSerial.h> 
#include <SparkFunESP8266WiFi.h> 



//#define GPSECHO false 

/////////////////// 
//GPS definitions// 
/////////////////// 

#include <Adafruit_GPS.h> 

//SoftwareSerial mySerial(3, 2); // 
//Adafruit_GPS GPS(&mySerial); 

////////////////////////////// 
// WiFi Network Definitions // 
////////////////////////////// 
// Replace these two character strings with the name and 
// password of your WiFi network. 
const char mySSID[] = "amg982"; 
const char myPSK[] = "ashleigh"; 

////////////////////////////// 
// ESP8266Server definition // 
////////////////////////////// 
// server object used towards the end of the demo. 
// (This is only global because it's called in both setup() 
// and loop()). 
ESP8266Server server = ESP8266Server(80); 

IPAddress comServer (192,168,43,151); 
////////////////// 
// HTTP Strings // 
////////////////// 
IPAddress destServer (127,0,0,1); 
const String htmlHeader = "HTTP/1.1 200 OK\r\n" 
          "Content-Type: text/html\r\n" 
          "Connection: close\r\n\r\n" 
          "<!DOCTYPE HTML>\r\n" 
          "<html>\r\n"; 

const String httpRequest = "POST /test.php\n" 
          "Host: 192.168.43.151\n" 
          "Connection: close\r\n"; 

// All functions called from setup() are defined below the 
// loop() function. They modularized to make it easier to 
// copy/paste into sketches of your own. 
void setup() 
{ 
    // Serial Monitor is used to control the demo and view 
    // debug information. 

///////////// 
//GPS SETUP// 
///////////// 

    Serial.begin(4800); 
// GPS.begin(9600); 
    // GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY); //recommended minimum only 
    // GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); //update [email protected] 
    // GPS.sendCommand(PGCMD_ANTENNA);// anntenna status update 



    /*******END******/ 

    serialTrigger(F("Press any key to begin.")); 

    // initializeESP8266() verifies communication with the WiFi 
    // shield, and sets it up. 
    initializeESP8266(); 

    // connectESP8266() connects to the defined WiFi network. 
    connectESP8266(); 

    // displayConnectInfo prints the Shield's local IP 
    // and the network it's connected to. 
    displayConnectInfo(); 

    serialTrigger(F("Press any key to connect client.")); 
    clientDemo(); 

    serialTrigger(F("Press any key to test server.")); 
    serverSetup(); 
} 

void loop() 
{ 
    serverDemo(); 
} 

void initializeESP8266() 
{ 
    // esp8266.begin() verifies that the ESP8266 is operational 
    // and sets it up for the rest of the sketch. 
    // It returns either true or false -- indicating whether 
    // communication was successul or not. 
    // true 
    int test = esp8266.begin(); 
    if (test != true) 
    { 
    Serial.println(F("Error talking to ESP8266.")); 
    errorLoop(test); 
    } 
    Serial.println(F("ESP8266 Shield Present")); 
} 

void connectESP8266() 
{ 
    // The ESP8266 can be set to one of three modes: 
    // 1 - ESP8266_MODE_STA - Station only 
    // 2 - ESP8266_MODE_AP - Access point only 
    // 3 - ESP8266_MODE_STAAP - Station/AP combo 
    // Use esp8266.getMode() to check which mode it's in: 
    int retVal = esp8266.getMode(); 
// if (retVal != ESP8266_MODE_STA) 
// { // If it's not in station mode. 
    // Use esp8266.setMode([mode]) to set it to a specified 
    // mode. 
    retVal = esp8266.setMode(ESP8266_MODE_STA); 
    // if (retVal < 0) 
    // { 
    // Serial.println(F("Error setting mode.")); 
     //errorLoop(retVal); 
    // } 
// } 
    Serial.println(F("Mode set to station")); 

    // esp8266.status() indicates the ESP8266's WiFi connect 
    // status. 
    // A return value of 1 indicates the device is already 
    // connected. 0 indicates disconnected. (Negative values 
    // equate to communication errors.) 
    retVal = esp8266.status(); 
    if (retVal <= 0) 
    { 
    Serial.print(F("Connecting to ")); 
    Serial.println(mySSID); 
    // esp8266.connect([ssid], [psk]) connects the ESP8266 
    // to a network. 
    // On success the connect function returns a value >0 
    // On fail, the function will either return: 
    // -1: TIMEOUT - The library has a set 30s timeout 
    // -3: FAIL - Couldn't connect to network. 
    retVal = esp8266.connect(mySSID, myPSK); 
    if (retVal < 0) 
    { 
     Serial.println(F("Error connecting")); 
     errorLoop(retVal); 
    } 
    } 
} 

void displayConnectInfo() 
{ 
    char connectedSSID[24]; 
    memset(connectedSSID, 0, 24); 
    // esp8266.getAP() can be used to check which AP the 
    // ESP8266 is connected to. It returns an error code. 
    // The connected AP is returned by reference as a parameter. 
    int retVal = esp8266.getAP(connectedSSID); 
    if (retVal > 0) 
    { 
    Serial.print(F("Connected to: ")); 
    Serial.println(connectedSSID); 
    } 

    // esp8266.localIP returns an IPAddress variable with the 
    // ESP8266's current local IP address. 
    IPAddress myIP = esp8266.localIP(); 
    Serial.print(F("My IP: ")); Serial.println(myIP); 
} 

void clientDemo() 
{ 
    // To use the ESP8266 as a TCP client, use the 
    // ESP8266Client class. First, create an object: 
    ESP8266Client client; 

    // ESP8266Client connect([server], [port]) is used to 
    // connect to a server (const char * or IPAddress) on 
    // a specified port. 
    // Returns: 1 on success, 2 on already connected, 
    // negative on fail (-1=TIMEOUT, -3=FAIL). 
    int retVal = client.connect(comServer, 80); 
    if (retVal <= 0) 
    { 
    Serial.println(F("Failed to connect to server.")); 
    return; 
    } 

    // print and write can be used to send data to a connected 
    // client connection. 
    client.print(httpRequest); 

    // available() will return the number of characters 
    // currently in the receive buffer. 
    while (client.available()) 
    Serial.write(client.read()); // read() gets the FIFO char 

    // connected() is a boolean return value - 1 if the 
    // connection is active, 0 if it's closed. 
    if (client.connected()) 
    client.stop(); // stop() closes a TCP connection. 
} 

void serverSetup() 
{ 
    // begin initializes a ESP8266Server object. It will 
    // start a server on the port specified in the object's 
    // constructor (in global area) 
    server.begin(); 
    Serial.print(F("Server started!")); 
    Serial.println(esp8266.localIP()); 
    Serial.println(); 
} 

void serverDemo() 
{ 

    float lic_no = 15; 
    // available() is an ESP8266Server function which will 
    // return an ESP8266Client object for printing and reading. 
    // available() has one parameter -- a timeout value. This 
    // is the number of milliseconds the function waits, 
    // checking for a connection. 
    ESP8266Client client = server.available(500); 

    if (client) 
    { 
    Serial.println(F("Client Connected!")); 
    // an http request ends with a blank line 
    boolean currentLineIsBlank = true; 
    while (client.connected()) 
    { 
     if (client.available()) 
     { 
     char c = client.read(); 
     // if you've gotten to the end of the line (received a newline 
     // character) and the line is blank, the http request has ended, 
     // so you can send a reply 
     if (c == '\n' && currentLineIsBlank) 
     { 
     if (client.connect(comServer, 80)==1) { 
      client.print("POST /test.php? "); // This 
      client.print("&lic_no="); // This 
      client.print(lic_no); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor 
      client.println("HTTP/1.1"); // Part of the GET request 
      client.println("Host: 192.168.43.151"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com") 
      client.println("Connection: close\r\n"); // Part of the GET request telling the server that we are over transmitting the message 
      client.println(); // Empty line 
      client.println(); // Empty line 
      client.stop(); // Closing connection to server 

      } 

     else { 
      // If Arduino can't connect to the server (your computer or web page) 
       Serial.println("--> connection failed\n"); 
      } 
     } 
     if (c == '\n') 
     { 
      // you're starting a new line 
      currentLineIsBlank = true; 
     } 
     else if (c != '\r') 
     { 
      // you've gotten a character on the current line 
      currentLineIsBlank = false; 
     } 
     } 
    } 
    // give the web browser time to receive the data 
    delay(1000); 

    // close the connection: 
    client.stop(); 
    Serial.println(F("Client disconnected")); 
    } 

} 

// errorLoop prints an error code, then loops forever. 
void errorLoop(int error) 
{ 
    Serial.print(F("Error: ")); Serial.println(error); 
    Serial.println(F("Looping forever.")); 
    for (;;) 
    ; 
} 

// serialTrigger prints a message, then waits for something 
// to come in from the serial port. 
void serialTrigger(String message) 
{ 
    Serial.println(); 
    Serial.println(message); 
    Serial.println(); 
    while (!Serial.available()) 
    ; 
    while (Serial.available()) 
    Serial.read(); 
} 
+0

당신에게 설정 한 변수 대신'$ _POST' 변수를 사용합니다. 그것이 그대로, 당신은 indicies가 설정되어 있는지 여부를 확인하더라도, 당신은 여전히 ​​그들이 아니더라도 그들을 액세스하려고합니다. –

+0

귀하의 스크립트는 [SQL Injection Attack] 위험에 있습니다. (http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) [ 리틀 바비 테이블] (http://bobby-tables.com/) 심지어 [당신이 입력을 탈출하는 경우, 안전하지 않습니다!] (http://stackoverflow.com/questions/5741187/sql-injection-that-gets -around-mysql-real-escape-string) [준비된 매개 변수가있는 문] (http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly

답변

0
$sql = 'INSERT INTO data '. 
     '(lic_no, speed, location) '. 'VALUES ("'.$_POST["lic_no"].'", "'.$_POST["speed"].'", "'.$_POST["location"].'")'; 

이것은해야

$sql = 'INSERT INTO data '. 
      '(lic_no, speed, location) '. 'VALUES ("'.$lic_no.'", "'.$speed.'", "'.$location.'")'; 
+0

그래도 적절한 이스케이프가 누락되었습니다. – ShiraNai7

+0

스칼라 변수를 사용하면 실제로 이점이 있습니다. 이것은 전혀 개선하지 못합니다. – RiggsFolly