tags:

views:

37

answers:

2
function add_new($father = 0 , $chName, $desc , $icon )  // add new category
{

$position  = $this->get_position($father);
$sql = "INSERT into ".$this->table_name."(position,c_name,c_desc,c_icon,c_group)
     VALUES('','".$chName."','".$desc."','".$icon."','".$this->Group."')";

mysql_query($sql) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));

$sql = "UPDATE ".$this->table_name."
     SET position = '$father'
     WHERE id = '".mysql_insert_id()."'";

mysql_query($sql) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));

}

Now my question is why does the UPDATE Query though works does not take the father variable. I am not sure whether its applicable.

+1  A: 

I believe variables having default values are to be placed at the end of the argument list.

function add_new($var1, $var2, $father = 0) { /*...*/ }

"Note that when using default arguments, any defaults should be on the right side of any non-default arguments; otherwise, things will not work as expected."

http://us.php.net/manual/en/functions.arguments.php

Jonathan Sampson
+3  A: 

You're not using the $position variable - and I don't see why you're doing two queries:

function add_new($father, $chName, $desc, $icon)  // add new category
{
    $position  = $this->get_position($father);
    $sql = "INSERT into ".$this->table_name."(position,c_name,c_desc,c_icon,c_group)
        VALUES('" . $position . "','".$chName."','".$desc."','".$icon."','".$this->Group."')";

    mysql_query($sql) or die('snip');
}

Additinally, default parameters only make sense at the end of the parameter list.

Greg