Help with code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joeros
Forum Newbie
Posts: 4
Joined: Mon Jun 14, 2010 12:13 am

Help with code

Post by joeros »

Hi,
I am new to php but i have experience in other disciplines of programming

i Have some code (free source) that i have adapted from Micheal Frank of this website
http://www.eyesis.ca (eyedatagrid)



I have changed what i need to suit my requirements but i am having problems updating and adding to a database. i have put code in the routine to echo the fact that that is where the code executes but apparently it does not

the code is as follows

THIS IS THE ORIGINAL

Code: Select all

$x->showCreateButton("alert('Code for creating a new person')", EyeDataGrid::TYPE_ONCLICK, 'Add New Person');
when the button is clicked the alert box pops up with the message 'Code for creating a new person'. i changed the code to what i THINK is correct

THIS IS MODIFIED

Code: Select all

$x->showCreateButton("insert('Tom,White,1959-20-10,f,25,'people')", EyeDataGrid::TYPE_ONCLICK, "Add New Item");
Note i have temporarily hardcoded the details to be added

I do not get an error with this syntax so i am thinking it is correct

i think that this is the code that is supposed to be called

Code: Select all

	/**
	 * Insert one new row
	 *
	 * @param array $values 3D array of fields and values to be inserted
	 * @param string $table Table to insert
	 * @return boolean Result
	 */
	public function insert(array $values, $table)
	{
		print "here";
		alert('Code for creating a new person');
		if (count($values) < 0)
			return false;
		
		foreach($values as $field => $val)
			$values[$field] = $this->escapeString($val);

		if ($this->query("INSERT INTO `" . $table . "`(`" . implode(array_keys($values), "`, `") . "`) VALUES ('" . implode($values, "', '") . "')"))
			return true;
		else
			return false;
	}

but when i click the web-page button nothing happens not even the print or the alert.

could anyone shed any light on this problem? It really is a neat bit of code and i can learn a lot from using this

Many thanks to any replies
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help with code

Post by requinix »

joeros wrote:I do not get an error with this syntax so i am thinking it is correct
Nope.

Code: Select all

insert('Tom,White,1959-20-10,f,25,'people')
I count three 's in there and three is an odd number.
joeros
Forum Newbie
Posts: 4
Joined: Mon Jun 14, 2010 12:13 am

Re: Help with code

Post by joeros »

Thanks for the reply and pointing out my error,

i changed the code to read

Code: Select all

$x->showCreateButton("insert('Tom,White,1959-20-10,f,25,'people'')", EyeDataGrid::TYPE_ONCLICK, "Add New Item");

that was an extra ' not a "
but it made no difference
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help with code

Post by requinix »

joeros wrote:that was an extra ' not a "
but it made no difference
Because it's still syntactically wrong.

Programming experience, right? So you're familiar with strings? Think about what you're doing in that bit of code.
joeros
Forum Newbie
Posts: 4
Joined: Mon Jun 14, 2010 12:13 am

Re: Help with code

Post by joeros »

tasiris, tks again

Hopefully i have the syntax correct now

Code: Select all

$x->showCreateButton("insert('Tom,White,1959-20-10,f,25','people')", EyeDataGrid::TYPE_ONCLICK, 'Add New Item');
i should mention that i have also tried this code

Code: Select all

$x->showCreateButton("insert('Tom','White','1959-20-10','f','25','people')", EyeDataGrid::TYPE_ONCLICK, 'Add New Item');
All to no avail.

Any ideas?
joeros
Forum Newbie
Posts: 4
Joined: Mon Jun 14, 2010 12:13 am

Re: Help with code

Post by joeros »

Ok,
it seems the main problem was not a syntactic error, the correct code is this

Code: Select all

("'Joe', 'Timbs', '1985-12-12', 'f', '50'")
looking at the code that has to process it, it calls for an array and due to my inexperience in PHP i thought i was sending an array. so the correct code is now this

Code: Select all

	$values = array ("'Joe', 'Timbs', '1985-12-12', 'f', '50'");
	insert ($values,"people");
the code is being passed to the following function the operative line being this

Code: Select all

	if ($this->query("INSERT INTO `" . $table . "`(`" . implode(array_keys($values), "`, `") . "`) VALUES ('" . implode($values, "', '") . "')"))
			return true;
		else
			return false;
Now, the first field of this table is an autoincrement number, so no matter what i pass to the function it will not update.

Can anyone now tell me how to add a record with this routine when the table uses an autoincrement field?

Thanks
Post Reply