php - MySQL update user code -
i have form updates users information upon submit. current setup allows form submitted if fields filled out. need create if statements each field says if populated, update, if not, dont.
i have password field listed below describes want each , every field, wasnt sure if list multiple variables inside if or have write separate if statements , select database every time
if($password != '') { if($password != $password2) { $error = '<div class="error_message">attention! passwords did not match.</div>'; } if(strlen($password) < 5) { $error = '<div class="error_message">attention! password must @ least 5 characters.</div>'; } if($error == '') { $sql = "update login_users set restricted = '$restrict', company_name = '$company_name', contact = '$contact', email = '$email', user_level = '$level', password = md5('$password') user_id = '$id'"; $query = mysql_query($sql) or die("fatal error: ".mysql_error()); echo "<h2>updated</h2>"; echo "<div class='success_message'>user information (and password) updated user id <b>$id ($company_name)</b>.</div>"; echo "<h2>what now?</h2><br />"; echo "<a href='xxxxxxxx'>« admin panel</a> | go <a href='user_edit.php'>edit users</a> page.</li>"; }
here more of code
if(trim($id) == '1') { $error = '<div class="error_message">attention! cannot edit main administrator, use database.</div>'; } else if(trim($company_name) == '') { $error = '<div class="error_message">attention! must enter company name.</div>'; } else if(trim($contact) == '') { $error = '<div class="error_message">attention! must enter contact name.</div>'; } else if(!isemail($email)) { $error = '<div class="error_message">attention! have entered invalid e-mail address, try again.</div>'; } else if(trim($level) == '') { $error = '<div class="error_message">attention! no user level has been selected.</div>'; }
i need create if statements each field says if populated, update, if not, dont.
you can build sql statement go. along lines of:
$sqlcols = ''; $error = ''; // password if ($password != '') { if ($password == $password2) { if (strlen($password) > 4) { $sqlcols .= "password = md5('".mysql_real_escape_string($password)."'), "; } else { $error .= '<div class="error_message">attention! password must @ least 5 characters.</div>'; } } else { $error .= '<div class="error_message">attention! passwords did not match.</div>'; } } // email if ($email != '') { if (isvalidemail($email)) { $sqlcols .= "email ='".mysql_real_escape_string($password)."', "; } else { $error .= '<div class="error_message">attention! email invalid.</div>'; } } if ($error == '') { $sql = "update login_users set ".trim($sqlcols, ', ')." user_id = '$id'"; // etc... }
in near future, switch on pdo improved performance , better protection against sql injection.
Comments
Post a Comment