簡體   English   中英

使用 php 將數據從 android 傳遞到 mysql

[英]Using php to pass data from android to mysql

Ive been trying to send gps data from my android app to a mysql database using php but Im not sure if everything is set up right because I can see through the logcat that the data is being sent from android but its just not getting sucked up by數據庫。

除了一些建議之外,任何機構都有任何教程可以專門顯示 php 到服務器代碼。

也可以在沒有 php 之類的情況下直接將數據發送到數據庫嗎? 我不這么認為,但我不得不問。

這是我正在使用的 php,出於明顯的原因,我使用了地址和內容。

<html>
<head>
<title>Send and Rec data to Android Device</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">


</head>


<body>


<?php
//include("dbinfo.php");



///the stuff pointing to my db has been removed for obvious ///reasons ;)


mysql_connect($hostname,$username,$password) or die ("<html><script           
language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-                
1)     </script></html>");


mysql_select_db($dbname);


// variables coming from Android  app on phone
$id = $_POST["id"];


$lat = $_POST["lat"]; // If data is in DB line 1 (public key), but the publicly displayed PHP is     
not showing data then we know that we aren't really talking to line 1

$lng = $_POST["lng"];


$alt = $_POST["alt"];


$spd = $_POST["spd"];


$acc = $_POST["acc"];


$last_gps = $_POST["lastgps"];


//filler will contain reference to graphic or audio upload directory
$filler = $_POST["filler"];


$lastid = mysql_query("select MAX(id) as maxid from samplemarks");
$row = mysql_fetch_array($lastid);


$nextid = $row[maxid] + 1;


echo "Inserting at next id = ".$nextid."<br />";


if ($lat == "") 

{
 echo "Invalid entry, latitude cannot be blank";

}

  else {
  $query2 = mysql_query("INSERT INTO samplemarks VALUES($nextid,'$lat',     
  $lng,'$alt','$spd','$acc','$last_gps','$filler')");

  mysql_query($query2);


mysql_close();


}
?>


</body>


</html>

您將在運行代碼或更正代碼時遇到問題,因為您將第一個 mysql_query() 的結果作為參數傳遞給第二個 mysql_query() 不起作用。 第一個 mysql_query 足以執行 SQL,請查看http://php.net/mysql_query以獲得一些好的代碼示例。 如果您只處理一個 mysql_connect() 連接,您也不需要 $conn 參數,因為 PHP 將假定您要使用最近打開的連接,如果您不指定它。

Also, make sure you do some good research on SQL injection attacks in PHP, particularly the article on the PHP site at http://php.net/manual/en/security.database.sql-injection.php - it's very important to在依賴用戶輸入時保護您的 SQL 查詢。

如果您遇到問題,請將其還原為基礎。 使用 print_r($_POST) 查看您的 POST 正文正在填充什么,並嘗試一些簡單的 mysql_query() 語句來檢查您的數據庫連接是否正常工作。

您的 mysql_query 缺少連接參數。 進行連接時,將其存儲在變量中,例如

$conn =  mysql_connect($hostname,$username,$password) or die('Blah');

if ($conn) {

    mysql_select_db("mydatabasename", $conn);

   // extract your data
   if (isset($_POST["id"]) && isset($_POST['lat']) && isset($_POST['lng']) {

           $id = $_POST['id'];
           $lat = $_POST['lat'];
           $lng = $_POST['lng'];

           // add the rest of the POST fields so the query below works
           // unless you need a specific $nextid make the field auto increment in the
           // database and user '' instead of $nextid.  

   $query2 = "INSERT INTO samplemarks VALUES($nextid,'$lat',     
     '$lng','$alt','$spd','$acc','$last_gps','$filler')";

    if (mysql_query($query2, $conn))
        echo "data inserted';
    else
        echo "Oh dear ".  mysql_error() ;


       // etc
  }  // end the isset tests
}

您的代碼中也沒有引用 $lng 。

我意識到您正在將數據從 android 應用程序發送到您的 php/db 服務器,但您應該始終檢查帖子中的數據,否則您很容易讓某人使用 sql 注入攻擊來破壞您的數據庫。 考慮使用 ssl 和用戶名/密碼保護您的 php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM