I store some values client side before sending them in a mysql database. Here's what I did.
I create a javascript array named "script" and I add some rows during the client session using this function :
var script = [];
function storestat(a,b,c,d){ script.push({Idcat: a, Idquest: b, Score: c, Equipe: d}); }
Then I send this data with ajax like this
function statquest(){
var postArray = JSON.stringify(script);
$.ajax({
url: 'statquest.php',
type: 'POST',
data: {data: postArray},
cache: false,
success: function(output){
dit = output;
},
error: function (request, status, error) {
}
});
}
In statquest.php, I get the data string and decode it like that :
$myarray = json_decode($_POST['data']);
Here's what i see if I use var_dump to show the object
array(2) { [0]=> object(stdClass)#3 (4) { ["Idcat"]=> string(1) "2" ["Idquest"]=> string(1) "4" ["Score"]=> int(3) ["Equipe"]=> int(5) } [1]=> object(stdClass)#4 (4) { ["Idcat"]=> string(1) "1" ["Idquest"]=> string(1) "6" ["Score"]=> int(3) ["Equipe"]=> int(2) } }
I want to insert this json object (all rows) into a mysql database which fields are named like in the object : Idcat, Idquest, Score and Equipe
I tried something like that but it doesn't work
$sql = "INSERT INTO ".$mydatabase." (`Idcat`, `Idquest`, `Score`, `Equipe`) VALUES (:Idcat,:Idquest,:Score,:Equipe)";
$q=$pdo->prepare($sql);
foreach($myarray as $row=>$value){
$q->bindValue(":".$row,$value);
}
$q->execute();
I have this error :
Catchable fatal error: Object of class stdClass could not be converted to string
Any idea someone ? Thank you