Hi,

I have a script to save Google Map markers in SQL.
The datas from "type" and "type2" should be compared to datas in existing table "type_table"

Unfortunatly the script does not save the datas. I always get the echo from "if($num_rows==0)", even if the gives datas from the formular match to "type" and "type2" in "type_table".

Can some please take a look to the script? I do not find the mistake... :(

Thanks a mill!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
 Copyright 2008 Google Inc. 
 Licensed under the Apache License, Version 2.0: 
 http://www.apache.org/licenses/LICENSE-2.0 
 -->
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Title</title>
    <script src="http://maps.google.com/maps?file=api&v=2&key=XXXXXXXXXXXXXXXXX"
            type="text/javascript"></script>

    <script type="text/javascript">

    var xmlHttp = false;

    if (typeof XMLHttpRequest != 'undefined') 
    {
    xmlHttp = new XMLHttpRequest();
    }
    if (!xmlHttp) 
    {
        try 
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e) 
        {
            try 
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e) 
            {
                xmlHttp = null;
            }
        }
    }

    var marker;
    function initialize() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(30.000000, 0.000000), 1);

        GEvent.addListener(map, "click", function(overlay, latlng) {
          if (latlng) {
            marker = new GMarker(latlng, {draggable:true});
            GEvent.addListener(marker, "click", function() {
              var html = "<table>" +
                         "<tr><td> </td> <td><input type='hidden' id='colour' value='Green'></td> </tr>" +
                         "<tr><td>Name:</td> <td><input type='text' id='name'/> </td> </tr>" +
                         "<tr><td>Date:</td> <td><input type='text' id='date'/></td> </tr>" +
                         "<tr><td>9-digit Number:</td> <td><input type='text' id='type'/></td> </tr>" +
                         "<tr><td>3-digit Number:</td> <td><input type='text' id='type2'/></td> </tr>" +
                         "<tr><td></td><td><input type='button' value='Save & Close' onclick='saveData()'/></td></tr>";

              marker.openInfoWindow(html);
            });
            map.addOverlay(marker);
          }
        });

      }
    }

    function saveData() {
      var colour = escape(document.getElementById("colour").value);
      var name = escape(document.getElementById("name").value);
      var date = escape(document.getElementById("date").value);
      var type = escape(document.getElementById("type").value);
      var type2 = escape(document.getElementById("type2").value);
      var latlng = marker.getLatLng();
      var lat = latlng.lat();
      var lng = latlng.lng();

    xmlHttp.open('POST', 'phpsqlinfo_addrow.php', true);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.onreadystatechange = messageresponse;
    xmlHttp.send("colour=" + colour + "&name=" + name + "&date=" + date + "&type=" + type + "&type2=" + type2 + "&lat=" + lat + "&lng=" + lng);

      GDownloadUrl("phpsqlinfo_addrow.php", function(data, responseCode) {
        if (responseCode == 200 && data.length <= 1) {
          marker.closeInfoWindow();
          document.getElementById("message").innerHTML = "Location added.";
        }
      });
    }

    function messageresponse()
    {
       if(xmlHttp.readyState==4) {
          if(xmlHttp.status==200) {
             marker.closeInfoWindow();
             document.getElementById('message2').innerHTML = xmlHttp.responseText;
          }
       }
    }

    </script>
  </head>

  <body onload="initialize()" onunload="GUnload()">
    <div id="map_canvas" style="width: 600px; height: 400px"></div>
    <div id="message"></div>
    <div id="message2"></div>
  </body>

</html>
<?php
require("phpsqlinfo_dbinfo.php");

$colour = $_GET['colour'];
$name = $_GET['name'];
$date = $_GET['date'];
$type = $_GET['type'];
$type2 = $_GET['type2'];
$lat = $_GET['lat'];
$lng = $_GET['lng'];

$connection = mysql_connect ("localhost", $username, $password);
if (!$connection) {
  die('Not connected : ' . mysql_error());
}

$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ('Can\'t use db : ' . mysql_error());
}

$query="Select * from type_table WHERE type='$type' AND type2='$type2' ";  
$result = mysql_query($query);  
$num_rows = mysql_num_rows($result);  

if($num_rows==0)  {
   echo $message1 = "Type or Type2 has been incorrect";
   } 

if($num_rows==1)  {
   $query = sprintf("INSERT INTO markers " . 
         " (id, colour, name, date, lat, lng, type, type2 ) " . 
         " VALUES (NULL, '%s', '%s', '%s', '%s', '%s', '%s', '%s');", 
         mysql_real_escape_string($colour), 
         mysql_real_escape_string($name), 
         mysql_real_escape_string($date), 
         mysql_real_escape_string($lat), 
         mysql_real_escape_string($lng), 
         mysql_real_escape_string($type),
         mysql_real_escape_string($type2)); 


   $result = mysql_query($query); 

   if ($result == true) 
   {
   echo $message2 = "Type and Type2 were correct and marker has been saved";
   } 

   if (!$result) { 
     die('Invalid query: ' . mysql_error()); 
   }  
}

?>

Moerkel,

It may not be the only thing but ....

Client-side: xmlHttp.open('POST', 'phpsqlinfo_addrow.php', true); ...
Server-side: $colour = $_GET['colour']; etc.
are incompatible.

Try: $colour = $_POST['colour']; etc.

Airshow

That´s it! Thanks a mill :)

I was looking for the answer in 3 german forums and no one had an answer for me...

Germans make very good cars :icon_lol:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.