UNIQUE用户名列未更新

时间:2017-05-24 01:33:06

标签: php mysql pdo

我正在为个人项目开发此注册表,当我尝试更新UNIQUE列时,没有任何反应。这是代码:

    <?php

   session_start();

   require 'database.php';

   $message = '';

   $olduser = $_POST['olduser'];
   $newuser = $_POST['newuser'];


   if ($olduser === "" || $newuser === ""){
     $message = "<div class='titleBox'>Please fill in all values.</div>";
   }

   else if(!empty($olduser) && !empty($newuser)):

   if(isset($_SESSION['user_id'])){

       $records = $conn->prepare('SELECT username FROM users WHERE username = :username');
       $records->bindParam(':username', $_SESSION['user_id']);
       $records->execute();
       $results = $records->fetch(PDO::FETCH_ASSOC);

   }

   if($_POST['username'] === $results['username']){

           $sql = "UPDATE users SET username='$newuser' WHERE username='$olduser'";

           if($conn->query($sql)){
               echo "<meta http-equiv='refresh' content='0'>";
           }       

       } else {
           $message = "<div class='titleBox'>Username may be incorrect or in use. Please try again.</div>";
       }

   endif;


   ?>

   <div id="contentCenter">




     <div class="subjectBox">Current Username:</div>

     <div class="titleBox"><?= $user['username'];?></div>
     <?php if(!empty($message)): ?><p><?= $message ?></p><?php endif; ?>

     <div class="subjectBox">Update Username:</div>


          <form action="edituser.php" method="post">
       <input type="text" name="olduser" placeholder="Enter old username here." class="titleBox" >
       <input type="text" name="newuser" placeholder="Enter new username here." class="titleBox" >
       <input type="submit" value="go." class="titleBox">
   </form>

   </div>

为了澄清,我希望在符合条件时更新用户名列。

1 个答案:

答案 0 :(得分:0)

我认为它是单引号中的变量,在这种情况下,变量不会被重新分配: $ sql =&#34; UPDATE users SET username =& #39; $ NEWUSER&#39;用户名=&#39; $ olduser&#39;&#34;;