1

ユーザー名、パスワード、電話、ユーザー生成IDなどを更新する更新スクリプトがあります。ユーザーが自分の電話を変更することを決定したが、ユーザー名は変更しないとしましょう。ただし、情報を編集する場合は、上記のすべてのフィールドがフォームに表示されます。したがって、ユーザーが変更したいものと一緒に送信されます。だから私はそれを検出するために以下のコードを使用しています。したがって、フィールドのユーザー名がセッションから取得したユーザー名と一致する場合は、続行しても問題がないことを意味します。一致しない場合は、ユーザー名が既に使用されていることを出力します。しかし、問題は、このクエリを2回実行する必要があることです。1回はユーザー名に対して、もう1回はユーザーによる一意のIDに対してです。さらに、更新スクリプトがページの下部にある場合は複雑になります。以下のコードはそれを検出しますが、エコーで停止します。

チェック1:

if ($username == $_POST['username') {
  echo "Good to go";
} else {
  echo "already in use";
}

チェック2:

if ($usergenid == $_POST['usergenid') {
  echo "Good to go";
} else {
  echo "already in use";
}
4

1 に答える 1

1

多分私は質問を誤解していますが、あなたはあなたの状態を入れ子にすることができますか?

オリジナル

$result = null;

if($username == $_POST['username']) 
{
   if($usergenid == $_POST['usergenid'])
   {

      /* perform update to user */
      $sql = 'UPDATE users SET username = ' . $_POST['username'] . ' WHERE id = ' . $_POST['usergenid'];
      if(mysql_query($sql))
      {
         $result = 'User updated';
      }
      else
      { 
         $result = 'Update failed';
      }

   }
   else
   {
      $result = 'User id already in use';
   }
} 
else 
{
  $result = 'Username already in use';
}

return $result;

user_idが一致する場合はユーザー名を更新します

$result = null;

if($usergenid == $_POST['usergenid'])
{
   if($username != $_POST['username']) 
   {

      /* perform update to user */
      $sql = 'UPDATE users SET username = ' . $_POST['username'] . ' WHERE id = ' . $_POST['usergenid'];
      if(mysql_query($sql))
      {
         $result = 'username updated';
      }
      else
      { 
         $result = 'update failed';
      }

   }
   else
   {
      $result = 'username is the same, no update performed';
   }
} 
else 
{
  $result = 'user id does not match';
}

return $result;
于 2011-01-31T08:03:13.413 に答える