php - 更新数据库中的字段

标签 php mysql

在我的管理页面中,我有一个可以编辑用户页面的表单。当我加载编辑页面和表单时,我还从数据库加载信息,因为如果字段为空,提交表单后它们在数据库中也会变为空。

问题出在密码字段上。加载表单时,密码字段是 password 并且显示如下 ••••••••••••••••••••••••• ••••••••••••••• 这是来自数据库4d9012b4a77a9524d675dad27c3276ab5705e5e8 的编码密码。如果我不更改密码并且没有输入相同的密码,该字段将在数据库中更新并变为此 8122c907fcf084364519b613b3ba6a3a88c9f980.. 这是要编辑的文件

// keep track post values
$username= $_POST['username'];
$password = sha1($_POST['password']);
$email = $_POST['email'];   

$fileName  = $_FILES['user_image']['name'];
$tmpName   = $_FILES['user_image']['tmp_name'];
$fileSize  = $_FILES['user_image']['size'];
$fileType  = $_FILES['user_image']['type'];

// make a new image name
$ext = substr(strrchr($fileName, "."), 1);
            // generate the random file name
$randName = date('Y-m-d') . '-' .$fileName;

// save image path
$path = "../../img/".$randName;
if (in_array($fileType, $permitted)) 
{
    $result = move_uploaded_file($tmpName, $path);
        if (!$result) 
        {
            echo "Error uploading image file";
            exit;
        } 
    }                       
    // update data
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    if ($fileName != null && $fileName != '')
    {
        $sql = "UPDATE users set username = ?, password = ?, email = ?, user_image = ? WHERE user_id = ?";
        $q = $pdo->prepare($sql);
        $q->execute(array($username,$password,$email,$path,$user_id));                          
    }
    else
    {
         $sql = "UPDATE users set username = ?, password = ?, email = ? WHERE user_id = ?";
         $q = $pdo->prepare($sql);
         $q->execute(array($username,$password,$email,$user_id));
    }
    if (isset($_POST)) {
        $_SESSION['edited'] = '<center><code>Done!</code></center>';
        } else {
            $_SESSION['edited'] = false;
        }
        header('Location: users.php');                      
    }               
    else 
    {
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "SELECT * FROM users where user_id = ?";
    $q = $pdo->prepare($sql);
    $q->execute(array($user_id ));
    $data = $q->fetch(PDO::FETCH_ASSOC);
    $username = $data['username'];
    $password= sha1($data['password']);
    $email = $data['email'];
    $user_image = $data['user_image'];
    Database::disconnect();
}                           
?>
<form role="form" action="" method="post" enctype="multipart/form-data">

<div class="form-group">
  User ID: <b><?php echo $user_id;?></b>
</div>

<!-- Text input-->
<div class="form-group">
  <label for="username">Username</label>
    <input value="<?php echo !empty($username)?$username:'';?>" id="username" name="username"  class="form-control" type="text">
</div>

<!-- File Button --> 
<div class="form-group">
  <label for="user_image">image</label>
    <input  id="userl_mage" name="user_image" class="input-file" type="file" value="<?php echo !empty($user_image)?$user_image:'';?>">
</div>

<!-- File Button -->            
<div class="form-group">
  <label for="password">Password</label>
    <input id="password" name="password" class="form-control" type="password" value="<?php echo !empty($password)?$password:'';?>">
</div>

<!-- Text input-->
<div class="form-group">
    <label for="email">Email</label>
    <input id="email" name="email" class="form-control" type="text" value="<?php echo !empty($email)?$email:'';?>">
</div>

我认为可能的解决方法是显示实际密码,以便在提交表单时密码不会更改为相同密码的哈希值。但我不想看到密码。

有什么制作方法吗?

最佳答案

让密码字段为空。存储的密码在任何情况下都应保持散列。当然你需要检查更新时密码字段是否设置,是否符合你的应用程序密码标准。

在您的编程逻辑中,您可以使用类似以下内容来确保发布密码:

if(isset($_POST["password"]) && !empty($_POST["password"])) {
    // update in database
}
else {
    // show error notification
}

请注意,这允许任何密码大小并且不是很安全。但它可以防止输入空密码。关于过滤数据的更多信息,我想this是一个很好的引用。

关于php - 更新数据库中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27964167/

相关文章:

mysql - 在 mariaDB 中完全连接 2 个表

mysql - mysql 查询出了什么问题?

mysql - 异常缓慢的行为 IN 子句 SQL

php - 使用 JOIN 获取有关两个用户的信息

php - Node.js `crypto.final` 使加密结果与 PHP `mcrypt_encrypt` 不同

php - 从 CSS 文件中提取@font-face 的字体系列和字体粗细

mysql - 将两个 SQL 查询的值相加以创建第三个值

php - array_push() 将一个值插入多个数组

php - 切http ://somelink/withparams and put it into <a href =""> tags

mysql - 将子查询(不在)重写为Join