2010-10-13 02:59
浏览 367


I have a table with 8 fields and a corresponding form. When the form is submitted, I have the id of the record. What is the best way to check if data in any one of the fields has changed for that row? Should use the SQL route

WHERE field1=$field1 AND field2=$field2 ..... AND ID=$ID

or pull the row and compare each field with a loop? Any other ways of achieving this?


图片转代码服务由CSDN问答提供 功能建议

我有一个包含8个字段和相应表格的表格。 提交表单时,我有记录的ID。 检查任何一个字段中的数据是否已针对该行更改的最佳方法是什么? 应该使用SQL路由

  WHERE field1 = $ field1 AND field2 = $ field2 ..... AND ID = $ ID 
   \  n 

或拉动行并用循环比较每个字段? 实现这一目标的任何其他方式?


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongwo5449 2010-10-13 03:10

    Depends on what you need to do...

    For example, some people check login details like so (assume all variables are escaped correctly).

    SELECT `id`
      FROM `users`
     WHERE `username` = $username
       AND `password` = $password

    But what happens if you want to log if the username was wrong or the password was wrong? (of course you should never tell the user this).

    In that case, I'd do...

    SELECT `id`, `password`
      FROM `users`
     WHERE `username` = $username

    If that fails, the username does not exist. You can then internally log this.

    Then you can do (sha1($salt . $password)) === $passwordFromDb) to be able to log if the username exists, but password was incorrect.

    However, if you don't need this level of logic, just compare in the SQL query. It will be more succinct and quicker to boot.

    打赏 评论

相关推荐 更多相似问题