我有JavaScript代码(jQuery和Ajax),我的AJAX引用的数据库和PHP页面。
我的主要目的是:
<div>
。我的目标是提取它们并将它们插入我的数据库。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
$(function(){
$("#button").on("click", function(){
$.ajax({
url: "myWebsiteName.com/receiveQuestion.php",
type: "post",
data: {
id_question: $("#id_question").html(), resultat_operation: $("#reponse").html()
},
success: function() {
alert('success')
},
error : function() {
alert('error')
},
})
var num1 = parseInt(document.getElementById("num1").innerHTML, 10);
var num2 = parseInt(document.getElementById("num2").innerHTML, 10);
var reponse = parseInt(document.getElementById("reponse").value, 10);
var signe = document.getElementById("signe").textContent;
...
(and there's a function verifying the results, etc...)
&#13;
引用的PHP页面代码在这里:
<?php
require('connexion.php');
require('fonctions.php');
if(isEnfant()) {
if(isset($_POST['id_question']) && isset($_POST['result_operation'])) {
try {
$id_questionInt = (int) $_POST['id_question'];
$result_operationInt = (int) $_POST['result_operation'];
$req = $bdd->prepare('INSERT INTO details (player, id_question_details, result_operation) VALUES (:id_player, :id_question, :resultOpe)');
$req -> bindValue(':id_player', $_SESSION['login_c']); // login of the children once he is online.
$req -> bindValue(':id_question', $id_questionInt);
$req -> bindValue(':resultOpe', $result_operationInt);
$req -> execute();
}
catch(PDOException $e) {
die('Error : ' . $e->getMessage());
}
}
}
?>
&#13;
我尝试了什么:
我知道请求通过邮件成功,但数据只是赢了将自己插入我的数据库。
答案 0 :(得分:0)
您可能需要使用PDO的类型常量之一绑定值。
$id_questionInt = intval($_POST['id_question']);
$result_operationInt = intval($_POST['result_operation']);
$login_c = $_SESSION['login_c'];
$req = $bdd->prepare('INSERT INTO details (player, id_question_details, result_operation) VALUES (:id_player, :id_question, :resultOpe)');
$req -> bindValue(':id_player', $login_c); // login of the children once he is online.
$req -> bindValue(':id_question', $id_questionInt, PDO::PARAM_INT);
$req -> bindValue(':resultOpe', $result_operationInt, PDO::PARAM_INT);
$req -> execute();