简体   繁体   English

如何在Google Maps API中访问此变量?

[英]How can I access this variable in Google Maps API?

I have a google maps web application for a game where the user will click a google map marker, make a selection in the window that pops up and click submit. 我有一个用于游戏的google maps网络应用程序,用户将单击google map标记,在弹出的窗口中进行选择并单击Submit。 It uses AJAX to update the database with information selected by the user. 它使用AJAX用用户选择的信息更新数据库。 The database is pre-populated with names of the markers and GPS coordinates, which are loaded. 数据库中已预先加载了标记的名称和GPS坐标。 The markers are also placed accordingly upon load via XML. 标记也通过XML相应地放置。

I'm having trouble updating one row in my DB called quest with the user selected information when it's submitted. 提交用户选择的信息时,我无法用自己选择的信息更新数据库中名为quest的一行。 Currently, a user can select a marker and submit a quest, but it won't update the DB at all. 当前,用户可以选择一个标记并提交一个任务,但是它根本不会更新数据库。 I'm unsure on the correct WHERE statement to use. 我不确定要使用的正确WHERE语句。 Here's my current SQL statement, I'm attempting to update a row called quest. 这是我当前的SQL语句,我正在尝试更新名为quest的行。

mysqli_query($con, "UPDATE markers SET quest= '$questName'  WHERE markerName = '$markerName'");

This is what happens when the submit button is pressed. 这就是按下提交按钮时发生的情况。

if (document.getElementById("questType").value == 
"quest1") { //if quest1 is selected upon submission
alert("quest1");
var markerName;
var questName = "Quest 1";
xmlhttp = new XMLHttpRequest();        
xmlhttp.open("GET", "ajax.php?questName=" + questName + "&markerName=" + markerName, true);         
xmlhttp.send(); //Sending the request to the server

ajax.php ajax.php

<?php 
include("connect.php");
require("call2.php");
$markerName = mysqli_real_escape_string($con, $_GET['markerName']);
$questName = mysqli_real_escape_string($con, $_GET['questName']);


$stmt = $con->prepare("UPDATE markers SET quest = $questName WHERE markerName = $markerName");
$stmt->bind_param($questName, $markerName);
$stmt->execute();
$stmt->close();
?>

Here is my relevant call file as well. 这也是我的相关通话文件。

$dom     = new DOMDocument("1.0");
$node    = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
include('connect.php');
$query  = "SELECT * FROM markers WHERE 1"; // Select all the rows in the markers table
$result = mysqli_query($con, $query);
if (!$result) {
die('Invalid query: ' . mysqli_error($con));
}

// Iterate through the rows, adding XML nodes for each
while ($row = mysqli_fetch_assoc($result)) {
global $dom, $node, $parnode;

$node    = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("markerName", 
$row['markerName']);
$newnode->setAttribute("quest", $row['quest']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("longg", $row['longg']);
}

header("Content-type: text/xml");
echo $dom->saveXML();

Sorry if this is a lot, I think the problem is i'm not assigning a value to markerName . 抱歉,如果很多,我认为问题是我没有为markerName分配值。 I don't get any errors, however when I hover over the ajaxphp in the network tab on chrome it looks like it's getting the questName but markerName remains undefined. 我没有收到任何错误,但是,当我将鼠标悬停在chrome的“网络”标签中的ajaxphp上时,看起来好像正在获得questName,但是markerName仍然未定义。

Here's where it's loading things in: 这是加载内容的位置:

downloadUrl("call2.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("markerName"); //<------ here's where it's getting markerName
//  var address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
  parseFloat(markers[i].getAttribute("lat")),
  parseFloat(markers[i].getAttribute("longg")));
var icon = customLabel[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
  });
  bindInfoWindow(marker, map, infoWindow, html);
}
});

I think I need to figure out a way to access name, but I'm unsure how to when it's local to the function which downloads the XML file. 我想我需要找出一种访问名称的方法,但是我不确定如何在下载XML文件的函数本地使用它。

connect.php connect.php

<?php
$con = mysqli_connect("localhost", "root", "", "pokestop-map");
?>

First of all, I think in some cases your Database Connection Credentials could be leaked if your Database is down and somebody is requesting ajax.php. 首先,我认为在某些情况下,如果您的数据库关闭并且有人请求ajax.php,则您的数据库连接凭据可能会泄漏。 If you want to use code in an production environment you should never leak errors to the client. 如果要在生产环境中使用代码,则永远不要将错误泄露给客户端。 Also I'm not sure if you're trying to add the Params to your Query through PHP's ability to evaluate variables in Strings, if so that is very unsecure and I would recommend you to read about Prepared Statements. 另外,我不确定您是否要通过PHP评估字符串中变量的功能来尝试将Params添加到查询中,如果这样做是非常不安全的,那么我建议您阅读Prepared Statements。 Also I would recommend you to use PDO's ( http://php.net/manual/de/book.pdo.php ) instead of MySQLi but that's just a sidenote. 我也建议您使用PDO( http://php.net/manual/de/book.pdo.php )代替MySQLi,但这只是一个旁注。 I think the Problem here is that you don't bind your value to the Query. 我认为这里的问题是您没有将值绑定到查询。 It's a long time ago that i've used MySQLi but I think you could do something like: 我很早以前就使用过MySQLi,但我认为您可以执行以下操作:

$stmt = $mysqli->prepare("UPDATE markers SET quest = ? WHERE markerName = ?");
$stmt->bind_param($questName, $markerName);
$stmt->execute();
$stmt->close();

Edit: I would recommend you to look at ORM libraries which allow you to Map the Rows in a table to an object in PHP. 编辑:我建议您查看ORM库,该库允许您将表中的行映射到PHP中的对象。 There are a few of them out there like Doctrine or Idiorm . 他们中有一些像教义成语 Working with Objects is much cleaner. 使用对象要干净得多。 But anyway I also wrote a little bit code which may help you, but I have to say its not tested. 但是无论如何,我也写了一点代码可能对您有帮助,但是我不得不说它未经测试。

DB.php (Holds an PDO in Singleton Pattern which will be used to communicate with the Database): DB.php(以单例模式持有一个PDO,它将用于与数据库进行通信):

require_once('Config.php');

class DB {
    private static $_instance = null;
    private $_pdo;

    private function __construct() {
        try {
            $this->_pdo = new PDO('mysql:host=' . Config::getDbHost() . ';dbname=' . Config::getDbName(), Config::getDbUser(), Config::getDbPass());
        } catch(PDOException $e) {
            die($e->getMessage());
        }
    }

    public static function getInstance() {
        if(!isset(self::$_instance)) {
            self::$_instance = new DB();
        }
        return self::$_instance;
    }

    /**
     * @return PDO
     */
    public function getPdo()
    {
        return $this->_pdo;
    }
}

Config.php (Holds the settings needed to connect to the Database, you can extend the Class and parse some kind of configuration file to fill the values if you want): Config.php(拥有连接数据库所需的设置,您可以扩展Class并解析某种配置文件以填充值):

class Config
{
    private static $_dbHost;
    private static $_dbName;
    private static $_dbUser;
    private static $_dbPass;

    /**
     * @return mixed
     */
    public static function getDbHost()
    {
        return self::$_dbHost;
    }

    /**
     * @return mixed
     */
    public static function getDbName()
    {
        return self::$_dbName;
    }

    /**
     * @return mixed
     */
    public static function getDbUser()
    {
        return self::$_dbUser;
    }

    /**
     * @return mixed
     */
    public static function getDbPass()
    {
        return self::$_dbPass;
    }
}

With these classes you should be able to do the following: 使用这些类,您应该能够执行以下操作:

<?php
require_once('DB.php');

$pdo = DB::getInstance()->getPdo();
$stmt = $pdo->prepare('UPDATE markers SET quest = :questName WHERE markerName = :markerName');

$stmt->bindValue('questName', $_GET['questName']);
$stmt->bindValue('markerName', $_GET['markerName']);

$stmt->execute();

As I said this is not tested, but it should work. 如我所说,这未经测试,但应该可以。 Maybe you have to fix includes and Parameter Binding... 也许您必须修复包含和参数绑定...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM