gpt4 book ai didi

php - 使用php将数据从android传递到mysql

转载 作者:太空宇宙 更新时间:2023-11-03 10:25:33 25 4
gpt4 key购买 nike

我一直在尝试使用 php 将 gps 数据从我的 android 应用程序发送到 mysql 数据库,但我不确定是否一切都设置正确,因为我可以通过 logcat 看到数据是从 android 发送的,但它只是没有得到被数据库吸走了。

除了一些建议之外,是否有任何机构有专门展示 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 会假设你想使用最近打开的连接。

此外,确保您对 PHP 中的 SQL 注入(inject)攻击做了一些很好的研究,尤其是 PHP 站点上的文章 http://php.net/manual/en/security.database.sql-injection.php - 当 SQL 查询依赖于用户输入时,保护它们非常重要。

如果您遇到问题,请将其剥离回到基础。使用 print_r($_POST) 查看您的 POST 正文被填充的内容,并尝试一些简单的 mysql_query() 语句来检查您的数据库连接是否正常工作。

关于php - 使用php将数据从android传递到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6115022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com