Function confusion II

Hi All,

I’m confused with a function behavior : it does accept 2 specific variables but doesn’t get 3 of them. I’ll try to show you the code to explain :


   function register($subuser, $subfullname, $subpass, $subpass_conf, $subemail, $txtNumber, $group, $category, $sub_category){
      global $database, $form, $mailer;  //The database, form and mailer object
      
      /* Username error checking */
      $field = "user";  //Use field name for username
      if(!$subuser || strlen($subuser = trim($subuser)) == 0){
         $form->setError($field, "* Username not entered");
      }
      else{
         /* Spruce up username, check length */
         $subuser = stripslashes($subuser);
         if(strlen($subuser) < 5){
            $form->setError($field, "* Username below 5 characters");
         }
         else if(strlen($subuser) > 30){
            $form->setError($field, "* Username above 30 characters");
         }
         /* Check if username is not alphanumeric */
         else if(!eregi("^([0-9a-z])+$", $subuser)){
            $form->setError($field, "* Username not alphanumeric");
         }
         /* Check if username is reserved */
         else if(strcasecmp($subuser, GUEST_NAME) == 0){
            $form->setError($field, "* Username reserved word");
         }
         /* Check if username is already in use */
         else if($database->usernameTaken($subuser)){
            $form->setError($field, "* Username already in use");
         }
         /* Check if username is banned */
         else if($database->usernameBanned($subuser)){
            $form->setError($field, "* Username banned");
         }
      }

      /* Fullname error checking */
      $field = "full_name";  //Use field name for username
      if(!$subfullname || strlen($subfullname = trim($subfullname)) == 0){
         $form->setError($field, "* Name not entered");
      }

      /* Password error checking */
      $field = "pass";  //Use field name for password
      if(!$subpass){
         $form->setError($field, "* Password not entered");
      }
      else{
         /* Spruce up password and check length*/
         $subpass = stripslashes($subpass);
         if(strlen($subpass) < 4){
            $form->setError($field, "* Password too short");
         }
         /* Check if password is not alphanumeric */
         else if(!eregi("^([0-9a-z])+$", ($subpass = trim($subpass)))){
            $form->setError($field, "* Password not alphanumeric");
         }
		 // CHECK RETYPE PASS VICTOR
		 else if($subpass != $subpass_conf) {
		 	$field = "pass_conf";
            $form->setError($field, "* Your 2 passwords did not match");
		 }
         /**
          * Note: I trimmed the password only after I checked the length
          * because if you fill the password field up with spaces
          * it looks like a lot more characters than 4, so it looks
          * kind of stupid to report "password too short".
          */
      }
      
      /* Email error checking */
      $field = "email";  //Use field name for email
      if(!$subemail || strlen($subemail = trim($subemail)) == 0){
         $form->setError($field, "* Email not entered");
      }
      else{
         /* Check if valid email address */
         $regex = "^[_+a-z0-9-]+(\\.[_+a-z0-9-]+)*"
                 ."@[a-z0-9-]+(\\.[a-z0-9-]{1,})*"
                 ."\\.([a-z]{2,}){1}$";
         if(!eregi($regex,$subemail)){
            $form->setError($field, "* Email invalid");
         }
         $subemail = stripslashes($subemail);
      }
	  
	  /* Randomimage error checking */
	  	$field = "safe_number";
		$safe_number = trim($txtNumber);

		if (md5($safe_number) != $_SESSION['image_random_value']) {
            $form->setError($field, "* Verification number was wrong!");
		} // end safe number NOT OK
		
		else {
		} // end safe number IS OK

	  /* Randomimage error checking END */
	  
	  /* GROUP, CATEGORY and SUBGROUP error checking */
		$field = "group";
		  if(!$group || strlen($group = trim($group)) == 0){
			 $form->setError($field, "* Group not choosen!");
		  }
		$field = "category";
		  if(!$category || strlen($category = trim($category)) == 0){
			 $form->setError($field, "* Category not choosen!");
		  }
		$field = "sub_category";
		  if(!$sub_category || strlen($sub_category = trim($sub_category)) == 0){
			 $form->setError($field, "* Sub-group not choosen!");
		  }
	  /* GROUP, CATEGORY and SUBGROUP error checking END */

      /* Errors exist, have user correct them */
      if($form->num_errors > 0){
         return 1;  //Errors with form
      }
      /* No errors, add the new account to the */
      else{
         if($database->addNewUser($subuser, $subfullname, $group, $category, $sub_category, md5($subpass), $subemail)){
            if(EMAIL_WELCOME){
               $mailer->sendWelcome($subuser,$subemail,$subpass);
            }
			if($database->addUserDetails($subuser, $subfullname, $group, $category, $sub_category)) {
				return 0;  //New user added succesfully
			}
			else {
				return 2;
			}
         }else{
            return 2;  //Registration attempt failed
         }
      }
   }

ERROR checking is ALL OK, you just ignore those lines :slight_smile:
And now here are the database functions :


   function addNewUser($username, $fullname, $group, $category, $sub_category, $password, $email){
      $time = time();
      /* If admin sign up, give admin user level */
      if(strcasecmp($username, ADMIN_NAME) == 0){
         $ulevel = ADMIN_LEVEL;
      }else{
         $ulevel = USER_LEVEL;
      }
      $q = "INSERT INTO ".TBL_USERS." VALUES ('$username', '$fullname', '$group', '$category', '$sub_category', '$password', '0', $ulevel, '$email', $time)";
      return mysql_query($q, $this->connection);
   }
   
   function addUserDetails($username, $fullname, $group, $category, $sub_category) {
	  $q_details = "INSERT INTO ".TBL_USERS_DETAILS." SET 
	  		username = '$username', 
			full_name = '$fullname',
			group = '$group'";
      return mysql_query($q_details, $this->connection);
   } // end function addUserDetails

Function addNewUser is working OK. Function addUserDetails is driving me crazy!!! When I pass only username and full_name it is working perfectly. BUT when I pass ‘group’ and the rest, the function fails… :injured:
I thought it was an error in the query (maybe it IS) and I’ve tried 100s of ways to write the query, nothing helped… It works (function addUserDetails) for 2 variables and fails for 5 of them. What’s my mistake, please help me!!!
THNX advanced!

Have you tried using mysql_real_escape_string() on your MySQL INSERT query? For example…

 '".mysql_real_escape_string($username)."'

Maybe there’s a character in your query that needs escaping.

   function addUserDetails($username, $fullname, $group, $category, $sub_category) {
      $q_details = "INSERT INTO ".TBL_USERS_DETAILS." SET 
              username = '$username', 
            full_name = '$fullname',
            group = '$group'";

echo $q_details ; // <- add this line
      return mysql_query($q_details, $this->connection);
   } 

Grab the resulting string, take a look at it, if it looks valid then paste it into your database management tool and check it does what it should.
The correct syntax is usually like this though:


INSERT INTO phonebook(phone, firstname, lastname, address)
VALUES ('+1 123 456 7890', 'John', 'Doe', 'North America')

taken from here: http://www.ntchosting.com/mysql/insert-data-into-table.html

I tried that. Didn’t help… Thanks anyway!

Thanks for the tip, but I think it should have returned an error in the case of the previous function (addNewUser) which is working ----> the same values are inserted into the first table (TBL_USERS)…

Is there a reason why you’re using INSERT INTO … SET syntax rather than INSERT INTO … VALUES()?

No! I tried


$q_details = "INSERT INTO ".TBL_USERS_DETAILS." VALUES ('$username', '$fullname', '$group')";

also. It’s not working, excpet for the first 2 variables as I said before… :confused:

Now this IS STRANGE!!! I tried with STATIC strings


$q_details = "INSERT INTO ".TBL_USERS_DETAILS." VALUES ('$username', '$fullname', group, category, sub)";

and it still doesn’t work… Can be a server issue???

What is the exact error you are seeing? Have you tried adding or die(mysql_error()) to the end of your mysql_query() function?

That was an intelligent and obvious step to make! Problem solved! It WAS an issue with the query. Thanks a lot!