Jump to content

Object of class player could not be converted to string


Vel

Recommended Posts

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.

 $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

 $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?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.