Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
Because your value is an object? $row equals the array index from $myarray, $value is everything in it, meaning the entire object (stdClass). What you're trying to achieve hier doesn't work anything like you think, you're trying to insert multiple rows using bound parameters...which is possible, but you should definitely read up on bulk insertion using prepared statements as well.. == EDIT: == And please do some checking! You accept a client side object as is without doing some sort of check first!! –  Kevin Op den Kamp May 27 at 11:52
    
I know about checking. I didn't post the full code to simplify... –  Abspirit May 27 at 12:02
    
You have this error on witch line? $q->bindValue(":".$row,$value); ? –  Portekoi May 27 at 12:38
    
Yes the error is on that line. Maybe i should change how I store datalines in my javascript array and not use an array of object... –  Abspirit May 27 at 12:48

2 Answers 2

up vote 0 down vote accepted

If i look your var_dump() :

{ [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) } }

Your row of the array "$myarray" is not a an array to : For example, if I want to read the first line :

echo $myarray[0]->Idcat;

---Edit to complete my reply-----

So, for your case, do something like that :

for($i=0;$i<sizeof($myarray);$i++){     
    foreach($myarray[$i] as $key => $val){
        $q->bindValue(":".$key,$val);
    }
    //Execute your query here
}
share|improve this answer
    
Yes it's an array of objects... How can i change my javascript or PHP "foreach" to make it work... –  Abspirit May 27 at 12:53
    
I've edit my reply to complete with the solution (I hope) –  Portekoi May 27 at 12:59
    
Thank you. That's working perfectly –  Abspirit May 27 at 13:15

You can convert your json data into php array by using the second argument of json_decode

$myarray = json_decode($_POST['data'], true);

$myarray will now be regular php array and can be used with foreach to do the rest of the operations.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.