Keep getting error Warning: PDOStatement::execute() expects at most 1 parameter, 2 given in config.php line 15

心已入冬 提交于 2019-12-02 12:29:48

问题


This is my index page for my geo location page

<!DOCTYPE html>
<html>
<head>
<script src="js/jquery.js"></script>
</head>

<body>
<script>
setInterval ( "onPositionUpdate()", 10000 );

var currPosition;
navigator.geolocation.getCurrentPosition(function(position) {
    updatePosition(position);
    setInterval(function(){
        var lat = currPosition.coords.latitude;
        var lng = currPosition.coords.longitude;
        $.ajax({
            type: "POST", 
            url:  "myURL/location.php", 
            data: 'x='+lat+'&y='+lng, 
            cache: false
        });
    }, 2000);
}, errorCallback); 

var watchID = navigator.geolocation.watchPosition(function(position) {
    updatePosition(position);
});

function updatePosition( position ){
    currPosition = position;
}

function errorCallback(error) {
    var msg = "Can't get your location. Error = ";
    if (error.code == 1)
        msg += "PERMISSION_DENIED";
    else if (error.code == 2)
        msg += "POSITION_UNAVAILABLE";
    else if (error.code == 3)
        msg += "TIMEOUT";
    msg += ", msg = "+error.message;

    alert(msg);
}
</script>
</body>
</html>

This is my location.php page

<?php
  include ('config.php');

  // database connection
  $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

  // new data

  $x = @$_POST['x'];
  $y = @$_POST['y'];

  // query
  $sql = "update locations set x=?, y=? where username = asd";
  $q = $conn->prepare($sql);
  $q->execute(array($x),($y));

?>

This is my config.php page

<?php
$dbtype     = "";
$dbhost     = "localhost";
$dbname     = "test";
$dbuser     = "root";
$dbpass     = "";

?>

The problem is when i am on my xampp testing my geo location out i keep getting a error Warning: PDOStatement::execute() expects at most 1 parameter, 2 given in C:\xampp\htdocs\project_track\myURL\location.php on line 15

I am trying to make a app that tracks my location and upload it to my database i been working on this project for some time hopefully i am on the right track i would like to know what do i do to correct this problem can some one please help...how should i set my database up for this i would like my app to record a username and lat and long and save it to my database and retrieve it so i can use it for google maps please help me with this...

Is my Sql wrong


回答1:


Your execute line should be:

$q->execute(array($x, $y));

Isn't it ? The Input parameter should be in form of array, but you supplied 2 parameters, one is array, the other one is a variable.

Also, the SQL is incorrect. It should be:

update locations set x=?, y=? where username = 'asd'

Reference: http://php.net/manual/en/pdostatement.execute.php



来源:https://stackoverflow.com/questions/17779649/keep-getting-error-warning-pdostatementexecute-expects-at-most-1-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!