2014-02-09 36 views
0

我的問題是什麼標題說我的表rowsetting浮法16,11爲經緯度 其他人是varchar奇怪的是我回聲出價值,這給了我究竟是什麼exspecting,但在數據庫中顯示0,00000000。其他值在PHP mSQL的感謝存儲我'新的正確的人的建議存儲谷歌座標到一個mysqli數據庫,但它只存儲零售

<?php require_once("includes/myDataBaseConnection.php"); ?> 
<?php require_once("includes/functions.php"); ?> 
<?php 


//postcode omzetten naar coordinaten 
function lookup($string){ 

    $string = str_replace (" ", "+", urlencode($string)); 
    $details_url = "http://maps.googleapis.com/maps/api/geocode/json?components=postal_code:".$string."|country:nl&sensor=false"; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $details_url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $response = json_decode(curl_exec($ch), true); 


    // If Status Code is ZERO_RESULTS, OVER_QUERY_LIMIT, REQUEST_DENIED or INVALID_REQUEST 
    if ($response['status'] != 'OK') { 
     return null; 
    } 


    $geometry = $response['results'][0]['geometry']; 
    $plaats = $response['results'][0]['address_components'][2]['long_name']; 
    $provincie = $response['results'][0]['address_components'][3]['long_name']; 
    $longitude = (float)$geometry['location']['lat']; 
    $latitude = (float)$geometry['location']['lng']; 

    $mapCor = array(
     'latitude' => (float)$geometry['location']['lng'], 
     'longitude' => (float)$geometry['location']['lat'], 
     // 'location_type' => $geometry['location_type'], 
     'plaats' => $response['results'][0]['address_components'][2]['long_name'], 
     'provincie' => $response['results'][0]['address_components'][3]['long_name'] 
    ); 

    return $mapCor; 

} 
    $postcode = $_POST['postcode']; 



    $mapCor = lookup($postcode); 




//query opslaan 

    foreach ($mapCor as $key => $value) { 
    print($value); 

    $query = "INSERT INTO markers ("; 
    $query .= "lat, lng, address, provincie"; 
    $query .= ") VALUES ("; 
    $query .= " '$value', '$value', '$value', '$value' "; 
    $query .= ")"; 
} 
    $result = mysqli_query($connection, $query); 

    if ($result) { 
     print ("succes!"); 

     //redirect_to("testGoogleMaps.php"); 
    } else { 
     // Failure 
     print ("Subject creation failed."); 


    } 

     if (isset($connection)) { mysqli_close($connection); } 

    ?>`enter code here` 
+0

在你的'foreach'循環您生成插入語句四次,但把value'一樣'$到每個領域。只有最後一次迭代用於下一行的查詢。 – 2014-02-09 23:38:10

+0

Thx爲你的快速回復你的權利,我看了一下:) –

回答

1
$mapCor = lookup($postcode); 

foreach ($mapCor as $key => $value) { 
    print($value); 
} 
$query = ' 
    INSERT INTO markers (`lat`, `lng`, `address`, `provincie`) 
    VALUES (\''.$mapCor['latitude'].'\', \''.$mapCor['longitude'].'\', \''.$mapCor['plaats'].'\', \''.$mapCor['provincie'].'\') 
'; 
+0

我會考慮使用混合''''''',甚至''''使事情更清晰可讀 – Floris

+0

嗨弗洛里斯thx爲你的快速回復這是解決方案thx很多新的語法,所以這是一個艱難的鬥爭,但我會保持你的意見再次thx很多。 –

+1

@弗洛伊斯我會建議使用PDO +準備,並避免'在全部:-P – Zac