2014-02-10 47 views
0

我正在研究一個'updateProfile'web服務,用戶可以更新一個或多個字段,我被難住爲如何形成查詢。我認爲如果isset條件可以有多個,並根據需要在內容上進行匹配。不過,我會碰上哪些是發佈的時間提前,因此不知道是否包括與運算或逗號等。這裏是我的出發點不知道的問題:如何更新一行中的多個字段,而不必知道哪些將被提前更新

$UpdateTradesman_Query= "UPDATE Tradesmen SET "; 
      if(isset($_REQUEST['FName'])){ 
       $FName=mysql_real_escape_string($_POST['FName']); 
       $UpdateTradesman_Query.= "FName ='".$FName."'"; 
      } 
      if(isset($_REQUEST['SName'])){ 
       $SName=mysql_real_escape_string($_POST['SName']); 
       $UpdateTradesman_Query.= "SName ='".$SName."'"; 
      } 
      if(isset($_REQUEST['Email'])){ 
       $Email=mysql_real_escape_string($_POST['Email']); 
       $UpdateTradesman_Query.= "Email ='".$Email."'"; 
      } 
      if(isset($_REQUEST['HomePhone'])){ 
       $HomePhone=mysql_real_escape_string($_POST['HomePhone']); 
       $UpdateTradesman_Query.= "HomePhone ='".$HomePhone."'"; 
      } 
      if(isset($_REQUEST['Mobile'])){ 
       $Mobile=mysql_real_escape_string($_POST['Mobile']); 
       $UpdateTradesman_Query.= "Mobile ='".$Mobile."'"; 
      } 
      if(isset($_REQUEST['Trade'])){ 
       $Trade=mysql_real_escape_string($_POST['Trade']); 
       $UpdateTradesman_Query.= "Trade ='".$Trade."'"; 
      } 
      if(isset($_REQUEST['Address'])){ 
       $Address=mysql_real_escape_string($_POST['Address']); 
       $UpdateTradesman_Query.= "Address ='".$Address."'"; 
      } 
      if(isset($_REQUEST['County'])){ 
       $County=mysql_real_escape_string($_POST['County']); 
       $UpdateTradesman_Query.= "County ='".$County."'"; 
      } 
      if(isset($_REQUEST['PostCode'])){ 
       $PostCode=mysql_real_escape_string($_POST['PostCode']); 
       $UpdateTradesman_Query.= "PostCode ='".$PostCode."'"; 
      } 
      if(isset($_REQUEST['Photo'])){ 
       $Photo=mysql_real_escape_string($_POST['Photoe']); 
       $UpdateTradesman_Query.= "Photo ='".$Photo."'"; 
      } 
      if(isset($_REQUEST['Profile'])){ 
       $Profile=mysql_real_escape_string($_POST['Profile']); 
       $UpdateTradesman_Query.= "Profile ='".$Profile."'"; 
      } 
      if(isset($_REQUEST['Qualification'])){ 
       $Qualification=mysql_real_escape_string($_POST['Qualification']); 
       $UpdateTradesman_Query.= "Qualification ='".$Qualification."'"; 
      } 
      if(isset($_REQUEST['AvailabilityTimes'])){ 
       $AvailabilityTimes=mysql_real_escape_string($_POST['AvailabilityTimes']); 
       $UpdateTradesman_Query.= "AvailabilityTimes ='".$AvailabilityTimes."'"; 
      } 

      $UpdateTradesman_Query.= "WHERE TradesmanID=".$TradesmanID; 
      echo $UpdateTradesman_Query; 
      if (mysql_query($UpdateTradesman_Query)){ 
       $status= 'OK'; 
       $message= 'null'; 
       $Update= "OK"; 
      } 
      else{ 

       $status= 'Not OK'; 
       $message= 'Error: Query Unsuccessful.'. mysql_error(); 
       $Update= "Not OK"; 
      } 

我也有考慮到如果用戶選擇更新用戶名或電子郵件,我必須確保:a)電子郵件尚未與另一個帳戶(已在數據庫中)關聯,或b)用戶名尚未被使用。任何幫助讚賞。

回答

0

與你目前有開始,你可以添加一個標誌,並添加逗號,如:

$addcomma=false; 

$UpdateTradesman_Query= "UPDATE Tradesmen SET "; 
     if(isset($_REQUEST['FName'])){ 
      $FName=mysql_real_escape_string($_POST['FName']); 
      $UpdateTradesman_Query.= "FName ='".$FName."'"; 
      $addcomma= true; 
     } 
     if(isset($_REQUEST['SName'])){ 
      $SName=mysql_real_escape_string($_POST['SName']); 
      if($addcomma) $UpdateTradesman_Query.= ","; 
      $UpdateTradesman_Query.= "SName ='".$SName."'"; 
      $addcomma= true; 
     } 
..etc.. 
+0

由於不正是我需要的。 – user2363025