1

我有一个更新脚本,可以更新用户名、密码、电话、用户生成的 ID 等。假设用户决定更改他/她的电话但不更改用户名。但是当他们去编辑他们的信息时,上面提到的所有字段都出现在表单上。因此,还与用户想要更改的内容一起提交。所以我使用下面的代码来检测它。因此,如果字段中的用户名与从会话中提取的用户名匹配,则表示可以继续,如果不是,则打印用户名已被使用。但问题是我必须运行此查询两次,一次用于用户名,一次用于用户的唯一 ID。此外,当更新脚本位于页面底部时,它会变得复杂。下面的代码将检测到它,但它们会在 echo 处停止。

检查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 回答