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
https://forums.phpfreaks.com/topic/95109-skip-empty-form-field/
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
https://forums.phpfreaks.com/topic/95109-skip-empty-form-field/#findComment-487232
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
https://forums.phpfreaks.com/topic/95109-skip-empty-form-field/#findComment-487233
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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