简体   繁体   English

比较数据库行和提交的表单数据的最佳方法

[英]The best way to compare a database row with a submitted form data

I have a table with 8 fields and a corresponding form. 我有一个包含8个字段和相应表格的表格。 When the form is submitted, I have the id of the record. 提交表单时,我具有记录的ID。 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 应该使用SQL路由

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? 还有其他方法可以做到这一点吗?

Thanks! 谢谢!

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. 然后,您可以执行(sha1($salt . $password)) === $passwordFromDb) ,以便能够记录用户名是否存在,但密码不正确。

However, if you don't need this level of logic, just compare in the SQL query. 但是,如果不需要这种逻辑级别,只需在SQL查询中进行比较即可。 It will be more succinct and quicker to boot. 它将更简洁,启动更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM