Connect with Expertise | Find Experts, Get Answers, Share Insights

Check if Input was filled, if not - continue with script

 
Join Date: Jun 2009
Posts: 43
#1: Dec 9 '09
Hiya. I have a script that is run by a <form> which basically allows u to write into inputs the fields username, new username, password, repeat password, comment etc,

The problem is that I want the script to work in a way that, if a field, like the 'new username' has not been touched (no value), then the username wont be changed.

The same kind of rule would go for the other inputs, so that only parts of a user can be edited at a time, without affecting other data.

Im using this script:

Expand|Select|Wrap|Line Numbers
  1. // EDIT USERNAEME
  2.  
  3.  if ((strlen($username)!==0)||(UserInDB($_POST["username"])))
  4.    //if(empty($_POST["newusername"]))
  5.    {
  6.     if (!(UserInDB($_POST["newusername"])))
  7.      {
  8.         mysql_query("UPDATE Users SET login='".$_POST["newusername"]."' WHERE login='".$username."'");
  9.         $oldname=$_POST["username"];
  10.         $newname=$_POST["newusername"];
  11.         echo $user_edited_name=' <b> The user <font color="#FFFFFF"><u>" '.$oldname.' "</u></font> was renamed to <font color="#FFFFFF"><u>" '.$newname.' "</u></font> in the user database.</b> ';
  12.         //echo '<meta http-equiv="Refresh" content="2; url=index.php"> ';
  13.      }
  14.    }
where
Expand|Select|Wrap|Line Numbers
  1.  if ((strlen($username)!==0)||(UserInDB($_POST["username"])))
Is supposed to check if the field newusername has been used,
and the function

Expand|Select|Wrap|Line Numbers
  1. if (!(UserInDB($_POST["newusername"])))
Checks if the user (from input "username") exists in the database.

The only problem is that the 'check if field was used' string doesnt work at all. If I want to update a different field, like "comment", I only input the username and leave newusername empty, but the user still gets renamed into " " (nothing).
You also see signs of trying the empty() function, which didnt help.
At least not how I tried it.


So how do I fix this?
Thanks

code green's Avatar
E
C
 
Join Date: Mar 2007
Location: England
Posts: 1,358
#2: Dec 10 '09

re: Check if Input was filled, if not - continue with script


Where does $username come from
Expand|Select|Wrap|Line Numbers
  1. if ((strlen($username)!==0
Do you have register globals switched on?
It looks like if $username is empty.
 
Join Date: Jun 2009
Posts: 43
#3: Dec 11 '09

re: Check if Input was filled, if not - continue with script


Oh yeah, how silly. That could be the error.

Edit: I checked my file and I actually have $username defined at the top, because it is used other places in the script.

I'm very new to PHP still, so could it be some character or symbol error in there?

This error is something that also occurs for different fields, like 'comment'.
So I cant simply update individual data on a user, since it just updates with the empty fields.

I really need some help to fix this :(
kovik's Avatar
E
C
 
Join Date: Jun 2007
Location: Baltimore
Posts: 844
#4: Dec 12 '09

re: Check if Input was filled, if not - continue with script


You are using "empty()" inside of your failing if-statement...

Using empty() is like checking if the variable is set and if it is non-empty and nonzero. The data you are checking is from a posted form, right? Then you only need to check in the $_POST array.

First, check if the $_POST array is non-empty. If so, then the form has been submitted. Secondly, check for the entry in the $_POST array that you are interested in. If it is non-empty, then it was successfully filled and submitted.

Also, $username should have no effect on the outcome, so I'm not sure why you are checking it...
Reply