简体   繁体   English

如何检查表中的密码是否更新

[英]HOW do I check if password is updated in a table

I have written a script in php to reset user's password, and how do I check if password is updated in a table?我在 php 中编写了一个脚本来重置用户的密码,如何检查表中的密码是否更新?

For example, if a data in the tuple/column has been changed, then send email.例如,如果元组/列中的数据已更改,则发送电子邮件。 Please check comments in the script.请检查脚本中的注释。

 $dbcc = mysqli_connect(HOST,NAME,PASSWORD,DATABASE) or die('Error can not connect to database');

 $query = "SELECT uid,email FROM `corporate` WHERE (email='$chk_email')";
 $result = mysqli_query($dbc, $query);
            
 //found
 if(@mysqli_num_rows($result) == 1)
 {
                    $ROW = mysqli_fetch_array($result);
                    $sent_email = $ROW['email']; //get email
                    $id = $ROW['uid'];           //get uid
                    
                    $new_password = generatePassword(8);//generates 8 char long random password 
                    $enc_password = md5($new_password); //encrypt
                    
                    $statement = "UPDATE corpoorate SET password=".$enc_password." WHERE uid ='$id'";
                    $go = mysqli_query($dbcc,$statement) or die(mysqli_error());
                    mysqli_close($dbcc);
                    
                       /*
                        * HOW DO I CHECK IF PASSWORD IS UPDATED IN THE DATABASE?
                        * IF IT IS, SEND EMAIL
                                                    * IF $go==true does not work!
                        **/
                    if($go==true){
                    $sendmessage = "We have generated a new password token for you.\n Your password is reset to ".$new_password." \n Please note that this password is not secure. Once you login, please reset your password.\n ";
                    
                     mail($sent_email,'Password Reset',$sendmessage,'From: address@gmail.com');     
                                                                                    }                   
                    
                     header("Location : http://limozoor.com/login/signin.php");
                     exit();    
        }//if
        mysqli_close($dbcc);

Why don't you use mysqli_affected_rows ?你为什么不使用mysqli_affected_rows

 // remove: $go = mysqli_query($dbcc,$statement) or die(mysqli_error());
 $qry =@ mysqli_query($dbcc, $statement);
 $aff =@ mysqli_affected_rows($dbcc);
 if ($qry === true && $aff > 0) {
      mail(...);
 }

From manual;来自手册;

mysqli_query : mysqli_query

Returns FALSE on failure.失败时返回 FALSE。 For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object.对于成功的 SELECT、SHOW、DESCRIBE 或 EXPLAIN 查询,mysqli_query() 将返回一个 mysqli_result 对象。 For other successful queries mysqli_query() will return TRUE.对于其他成功的查询,mysqli_query() 将返回 TRUE。

mysqli_affected_rows : mysqli_affected_rows

An integer greater than zero indicates the number of rows affected or retrieved.大于零的整数表示受影响或检索的行数。 Zero indicates that no records where updated for an UPDATE statement, no rows matched the WHERE clause in the query or that no query has yet been executed.零表示没有为 UPDATE 语句更新的记录、没有与查询中的 WHERE 子句匹配的行或尚未执行任何查询。 -1 indicates that the query returned an error. -1 表示查询返回错误。

http://php.net/manual/en/mysqli.affected-rows.php http://php.net/manual/en/mysqli.affected-rows.php
http://php.net/manual/en/mysqli.query.php http://php.net/manual/en/mysqli.query.php

Because of your or die(mysqli_error());因为你or die(mysqli_error()); -condition the password will always be updated in the table if it reaches those lines of execution. -condition 密码将始终在表中更新,如果它到达那些执行行。

However, I am sceptic towards your if(@mysqli_num_rows($resultt) == 1) because if there is any error in your first SQL-query, you are supressing all error messages there (by using @ ), which makes me think that you never even try to execute the UPDATE statements.但是,我对您的if(@mysqli_num_rows($resultt) == 1)持怀疑态度,因为如果您的第一个 SQL 查询中出现任何错误,您就会在那里(通过使用@ )抑制所有错误消息,这让我认为您甚至从未尝试执行 UPDATE 语句。

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

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