繁体   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