0

below is my code. it calculates the coordinates of the user, and is supposed to send it to my DB. the calculation takes some time so i set the redirect timer to 9 seconds. the calculations finish and my coordinates are shown, but still nothing is added to my DB. when i change the bound value of 5 and 6. it does work.

<?php
    $lat='
    <script>
        var lat;
        var lng;
        navigator.geolocation.getCurrentPosition(showPosition);
        function showPosition(position) {
            lat = position.coords.latitude;
            document.writeln(lat);
        }
    </script>';

    $lng='
    <script>
        var lat;
        var lng;
        navigator.geolocation.getCurrentPosition(showPosition);
        function showPosition(position) {
          lng = position.coords.longitude;
          document.writeln(lng);
        }
    </script>';

    $addinfo = strip_tags($_POST["detailthing"]);
    $titel   = strip_tags($_POST["titel"]);
    $type    = $_POST["welke"];

    session_start();
    include("opendb.php");
    echo $lat;
    echo $lng;
    $stmt = $db->prepare('INSERT INTO requests (type, user_id, addinfo, title, lat, lng) VALUES ( ? , ? , ? , ? , ? , ? )');
    $stmt->bindValue(1, $type, PDO::PARAM_STR);
    $stmt->bindValue(2, $_SESSION["user_id"], PDO::PARAM_STR);
    $stmt->bindValue(3, $addinfo, PDO::PARAM_STR);
    $stmt->bindValue(4, $titel, PDO::PARAM_STR);
    $stmt->bindValue(5, $lat, PDO::PARAM_STR);
    $stmt->bindValue(6, $lng, PDO::PARAM_STR);
    $stmt->execute();

    header("refresh: 9;url=https://agile130.science.uva.nl/#linkposts");
?>
MaxiGui
  • 6,190
  • 4
  • 16
  • 33
  • Where do you get the post parameters from? – Shadow Jan 27 '21 at 16:09
  • This won't solve your problem, but you don't actually have to use `bindValue` for PDO. You could just pass an array of values in the correct order to `execute()`. – GrumpyCrouton Jan 27 '21 at 16:10
  • @Shadow from another file ( index.php) – robin reitsma Jan 27 '21 at 16:13
  • 4
    `$lng=' – El_Vanja Jan 27 '21 at 16:18
  • @El_Vanja yes. the script retrieves the user location, which is stored in the php variable – robin reitsma Jan 27 '21 at 16:19
  • 1
    That's not how web works. You need to learn about the [difference in client and server side programming](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming). You'll need AJAX to achieve what you want. – El_Vanja Jan 27 '21 at 16:20
  • `the script retrieves the user location`...sure, but when the query is executed, the Javascript scripts haven't been executed yet! It will be trying to insert the ` – ADyson Jan 27 '21 at 16:22
  • All you've done here is create some PHP string variables, which happen to contain some Javascript code. Then you try to insert those strings into your database. I'm guessing you're probably trying to insert them into integer fields, which will cause the query to fail (but if you enabled PDO and PHP error handling, you'd know the error for sure). You need to echo the scripts to the page, let them execute, and then send a request back to the server containing the result...only then, on that second request, can you execute your SQL query. – ADyson Jan 27 '21 at 16:23
  • @ADyson Actually, they are echoed, there are two lines `echo $lat;` and `echo $lng;`, right before the query. The rest stands, though. – El_Vanja Jan 27 '21 at 16:25
  • @ADyson thanks for the response, turns out this is more complicated than i initially thought. – robin reitsma Jan 27 '21 at 16:28
  • @El_Vanja thanks for the link! – robin reitsma Jan 27 '21 at 16:29
  • @El_Vanja just spotted that before you and edited the comment :-) – ADyson Jan 27 '21 at 16:29

0 Answers0