Vel Posted October 12, 2011 Share Posted October 12, 2011 I'm receiving the error "Object of class player could not be converted to string" yet I cannot find why what I'm inputting is an object and not a string. As far as I can see it is a string and this error shouldn't be happening, yet it is... A form submits to a page which gets the data thus: <?php case "updplayer": if(!isset($_GET['id']) || !isset($_POST['kit']) || !isset($_POST['level']) || !isset($_POST['group']) || !isset($_POST['state'])) { header("location:admin.php?mode=settings&error=info&sid=$user->sid"); exit; } $var = "player" . $_GET['id']; $$var->kit = mysql_real_escape_string($_POST['kit']); $$var->level = mysql_real_escape_string($_POST['level']); $$var->group = mysql_real_escape_string($_POST['group']); $$var->state = mysql_real_escape_string($_POST['state']); $$var->update(); header("location:admin.php?sid=$user->sid"); exit; break; The update function is: <?php function update() { echo "Name: $this->name <br /> Kit: $this->kit <br /> Level: $this->level <br /> Group: $this->group <br /> State: $this->state <br />"; $sql = "UPDATE `players` SET `name` = '" . $this->name . "', `preferredKit` = '" . $this-kit . "', `gamingLevel` = '" . $this->level . "', `group` = '" . $this->group . "', `draftState` = " . $this->state; if(!mysql_query($sql)) { return mysql_error(); } return TRUE; } I added the echo's in for error checking. They output exactly what was expected. What is wrong with this? Error occurs on the "$sql = " line. Quote Link to comment https://forums.phpfreaks.com/topic/248975-object-of-class-player-could-not-be-converted-to-string/ Share on other sites More sharing options...
requinix Posted October 12, 2011 Share Posted October 12, 2011 And what does print_r($$var); output? Quote Link to comment https://forums.phpfreaks.com/topic/248975-object-of-class-player-could-not-be-converted-to-string/#findComment-1278655 Share on other sites More sharing options...
ManiacDan Posted October 12, 2011 Share Posted October 12, 2011 $this-kit Typo right there. Also, if you were working with errors turned on, it would have told you "undefined constant 'kit'" and you could have spotted this right away. Also also, you're doing this wrong. Don't use variable variables. Put them in an array -Dan Quote Link to comment https://forums.phpfreaks.com/topic/248975-object-of-class-player-could-not-be-converted-to-string/#findComment-1278659 Share on other sites More sharing options...
Vel Posted October 12, 2011 Author Share Posted October 12, 2011 $this-kit Typo right there. Also, if you were working with errors turned on, it would have told you "undefined constant 'kit'" and you could have spotted this right away. Also also, you're doing this wrong. Don't use variable variables. Put them in an array -Dan Thank you! I have been through that code line by line so many times I don't know how I missed that. I turned errors on on my index page but forgot to turn them on this page. I have turned them on now. I didn't know you could put them in an array. So when creating it, instead of using <?php $var = "var" . $i; $$var = new class; Would I use? <?php $var = array(); $var[$i] = new class; Is that right? Quote Link to comment https://forums.phpfreaks.com/topic/248975-object-of-class-player-could-not-be-converted-to-string/#findComment-1278695 Share on other sites More sharing options...
ManiacDan Posted October 12, 2011 Share Posted October 12, 2011 1) Turn error reporting all the way up in PHP.ini and leave it there. 2) Yes, that code is preferable because you have all your objects in one variable and you can delete/modify that whole array when you need to rather than attempting to use variables that may or may not exist. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/248975-object-of-class-player-could-not-be-converted-to-string/#findComment-1278721 Share on other sites More sharing options...
Vel Posted October 12, 2011 Author Share Posted October 12, 2011 I've set error reporting to E_ALL | E_STRICT in php.ini now. As you can probably guess I'm still new to all this so thank you for the help and advice. Now to recode my variable variables into an array. Quote Link to comment https://forums.phpfreaks.com/topic/248975-object-of-class-player-could-not-be-converted-to-string/#findComment-1278722 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.