我有一個表單提交時更新用戶信息。當前設置只允許填寫所有字段時提交表單。我需要爲每個字段創建if語句,如果填充,更新,如果不是,不要。MySQL更新用戶代碼
我有下面列出的密碼字段,描述了我想要爲每個字段做什麼,但我不確定是否可以列出IF中的多個變量,還是必須編寫單獨的IF語句,並從數據庫每次
if($password != '') {
if($password != $password2) {
$error = '<div class="error_message">Attention! Your passwords did not match.</div>';
}
if(strlen($password) < 5) {
$error = '<div class="error_message">Attention! Your password must be at 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')
WHERE 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 for User ID <b>$id ($company_name)</b>.</div>";
echo "<h2>What to do now?</h2><br />";
echo "<a href='xxxxxxxx'>« Back to Admin Panel</a> | Go to the <a href='user_edit.php'>edit users</a> page.</li>";
}
下面是一些我的代碼
if(trim($id) == '1') {
$error = '<div class="error_message">Attention! You cannot edit the main Administrator, use database.</div>';
} else if(trim($company_name) == '') {
$error = '<div class="error_message">Attention! You must enter a company name.</div>';
} else if(trim($contact) == '') {
$error = '<div class="error_message">Attention! You must enter a contact name.</div>';
} else if(!isEmail($email)) {
$error = '<div class="error_message">Attention! You have entered an invalid e-mail address, try again.</div>';
} else if(trim($level) == '') {
$error = '<div class="error_message">Attention! No user level has been selected.</div>';
}
如果你想爲不同的字段使用不同的消息,你需要多個if語句,是的。 – joakimdahlstrom
那麼,我需要它不更新,如果沒有填寫,但我也不希望它更新我的表與一個空白字段 – iamwhitebox
好吧,我會寫一些東西在一分鐘。 – joakimdahlstrom