Jump to content

Skip empty form field


Xeven

Recommended Posts

Is it possible to skip an empty form field when inserting or updating values in a mysql table?

 

Example:

A page where a user can change his/her details.  However something such as a password field may not want to be changed, but the rest of the values do need to be changed.

 

So when the form is submitted, the fields which have had data entered into them are updated on the table, but the field password is still left the same on the table and not inserted with a null value.

 

Possible or wishful thinking?

 

Thanks

Link to comment
Share on other sites

You may want to prefill the fields in the forms with the data that is already in the DB. That is usually how it is done for profiles & such.

 

Does that count even for a password field?  I would have thought that still would have been left blank.

Link to comment
Share on other sites

hmm... It should put the values in it and bleep them out also. I'm not sure.

 

See what this displays:

 

$password = "valuefromDB";

<input type="password" value="$password" name="password">

 

do a print_r($_RESULT); to see what value is there.

Link to comment
Share on other sites

Just check if the password part of the form meets the criteria for a password, and if it does, update it.

 

Example:

 


$password = (isset($_POST['password'])) ? trim($_POST['password']) : '';

if(!empty($password)) {
     //update password
     //note that this just checks if a password was entered.... no other checks are done x.x.
}

Link to comment
Share on other sites

if ($password == "") {

// create query
$query = "UPDATE users SET firstname='$firstname', lastname='$lastname', address='$address', city='$city', state='$state', zip='$zip', Email='$Email', MSN='$MSN', Yahoo='$Yahoo', AOL='$AOL', Gmail='$Gmail', phone='$phone', private='$private' WHERE username = '$_SESSION[username]'";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

}

else {

// Create a salt to add.
$salt = ($saltpass);

// Create Hash of password and salt.
$passwordHash = hash('SHA512', $password.$salt);

// create query
$query = "UPDATE users SET password='$passwordHash', firstname='$firstname', lastname='$lastname', address='$address', city='$city', state='$state', zip='$zip', Email='$Email', MSN='$MSN', Yahoo='$Yahoo', AOL='$AOL', Gmail='$Gmail', phone='$phone', private='$private' WHERE username = '$_SESSION[username]'";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

}

 

That is what my user edit page looks like

Link to comment
Share on other sites

You guys are amazing.  It is now up and working.  Thanks for all of you who helped out and gave idea's on what I could do.

 

Peranha, your stuff gave me a lot of insight on how I should go about doing it, thanks for posting your code.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.