2011-07-01 87 views
1

我有一個表單提交時更新用戶信息。當前設置只允許填寫所有字段時提交表單。我需要爲每個字段創建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'>&laquo; 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>'; 
} 
+0

如果你想爲不同的字段使用不同的消息,你需要多個if語句,是的。 – joakimdahlstrom

+0

那麼,我需要它不更新,如果沒有填寫,但我也不希望它更新我的表與一個空白字段 – iamwhitebox

+0

好吧,我會寫一些東西在一分鐘。 – joakimdahlstrom

回答

1

我需要創建if語句爲 說,如果填充每個領域, 更新,如果沒有,不要。

您可以隨時構建SQL語句。沿着線的東西:

$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! Your password must be at least 5 characters.</div>'; 
     } 
    } else { 
     $error .= '<div class="error_message">Attention! Your 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! Your email is invalid.</div>'; 
    } 
} 

if ($error == '') { 
    $sql = "UPDATE login_users 
      SET ".trim($sqlCols, ', ')." 
      WHERE user_id = '$id'"; 

    // etc... 
} 

在不久的將來,切換到PDO以改善性能和針對SQL注入更好的保護。

+0

所以,如果我有一個$用戶名,$名稱,$電子郵件等...我會繼續if語句? – iamwhitebox

+0

我只是想圍繞這個 – iamwhitebox

+0

是的。我添加了一個電子郵件段來說明。另外,由於用戶可以提交產生多個錯誤的表單,因此我已經連接了錯誤字符串。 – webbiedave

1

這取決於您想要什麼樣的用戶反饋,但是這裏有一個簡單的方法來收集通過驗證的字段,並將它們用於查詢。

$errors = array(); 
$fields = array(); 

if(($password != $password2) { 
    $errors[] = "Passwords didn't match"; 
    $fields['password'] = $password; 
} 

if(empty($email)) { 
    $errors[] = "Email is empty"; 
    $fields['email'] = $email; 
} 

if($something > $nothing) { // 
    $errors[] = "More errors"; 
    $fields['something'] = $something; 
} 

//and so on... 

if(!count($errors)) { 

    $str = ''; 

    foreach($fields as $field => $val) { 
     $str .= $field. "= '" .$val."', "; 
    } 

    $str = substr($str,0,1); //removes last , (comma) 

    $sql = "UPDATE login_users 
     SET $str 
     WHERE user_id = '$id'"; 

    //do query.. 

}