I m having a issue with binding paramters in PDO. This problem happens if I want to use two out parameters from a javascript. With the use of .AJAX the variables are send to the PHP script.(I tested of the javascript variables are available in my PHP script and they were indeed.)
I don't get a error message when I use this variables but the SQL isn't executed. When I replace the javascript variables with a integer like '12344' it works. The code that i m using is:
session_start();
$user = 'postgres';
$pass = 'baf45baf';
$dans = $_SESSION['dans_code'];
if( isset($_POST['ycoord'], $_POST['xcoord']) ) {
$dbh = new PDO("pgsql:host=localhost;dbname=import", $user, $pass);
$sql = 'UPDATE geom_tijd SET x_coord= :x_coord, y_coord= :y_coord WHERE dans_code = :code';
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':code', $dans);
$stmt->bindValue(':y_coord', $_POST['ycoord'],PDO::PARAM_INT);
$stmt->bindValue(':x_coord', $_POST['xcoord'],PDO::PARAM_INT);
$stmt->execute();
if (!$stmt) {
echo "\nPDO::errorInfo():\n";
print_r($dbh->errorInfo());
}
if($stmt->rowCount() == 0){
echo "failure";
} else {
echo "victory";
}
}
If I echo the xcoord I get '212562.46720' and the ycoord '520411.55763'.
The $dans variable is a variable that I'm getting from $_SESSION['dans_code']; This variable is from a php script earlier and its working if I'm only using this variable.
This is the part of my javascript that get and also send the javascript value. The values are the x and y coordinates from the mouseposition onclick in the map. This is not the best way to get this variables but it works.
map.on('click', function() {
var show = document.getElementById("location").innerHTML;
var show_schoonmaken = show.replace('<div class="custom-mouse-position">',"");
var show_klaar = show_schoonmaken.replace('</div>',"");
var show_done = show_klaar.replace(' ',"");
var split = show_done.split(',');
document.getElementById("y_coördinaat").value = split[1];
document.getElementById("x_coördinaat").value = split[0];
$.ajax({
url: 'update_coordinates.php', //This is the current doc
type: "POST",
data: ({xcoord: split[0],ycoord: split[1]}),
success: function(data){
console.log(data);
alert(data);
}
});
});