无法更新用户名值。 PHP / MySQL的

时间:2013-04-30 17:39:16

标签: php mysql

我有以下问题。当我尝试通过HTML表单更新用户名值时。 这是我的HTML表单:

<form action="namechanging.php" method="post">

<br>
  <fieldset>
    <span class="ico user-ico"></span>
     <input name="ingamename" type="text" class="field" value="Example: John_Doe" title="Example: John_Doe" />
   </fieldset>
   <center><input name="submit" type="submit" class="submit btn blue-btn" value="Change Name" /></center>
</form>

这是我的namechanging.php文件:

<?php 
  session_start();
  $_SESSION['ingamename'] = $_POST['ingamename']; 
  $newingamename = $_SESSION['ingamename'];
  $nc = $_SESSION['nc'];
  if($nc < 1){
    $updateresult = '<strong>Error:</strong> You do not have any name changes left. Donate for more...'; 
    header("Location: ucp-home-error.php");
  }
  else {
    $con=mysqli_connect("localhost","USERNAME","PASS","DB-NAME");
    if (mysqli_connect_errno())
    {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $newnc = $nc - 1;

    mysqli_query($con,"UPDATE users SET Username=$newingamename WHERE Username='$_SESSION[user]' ");
    mysqli_query($con,"UPDATE users SET NameChanges=$newnc
         WHERE Username='$_SESSION[user]'");
    $updateresult = 'Your In-Game Name was changed successfully. Please re-log!';
    header("Location: ucp-home-success.php");
  }
  mysqli_close($con);
  $_SESSION['updateresult'] = $updateresult;?>

它可以从$ nc中删除-1,但它不会更改用户名。您还应该知道用户名已保存在会话中,这是我登录到身份验证页面的用户名。 这是我的auth.php文件:

<?php
include("sql.php");
session_start();

function Destroy() {
  unset($_SESSION['UID']);
  unset($_SESSION['USERNAME']);
  unset($_SESSION['FULL NAME']);
  header("location: account-log-in-restricted.php");
}

if(isset($_SESSION['UID']) && isset($_SESSION['USERNAME'])) {
  $UID = $_SESSION['UID'];
  $username = $_SESSION['USERNAME'];
  $qry = mysql_query("SELECT * FROM `users` WHERE `UID` = '$UID' AND `Username` = '$username'");
  if(mysql_num_rows($qry) != 1) { Destroy(); }
} else { Destroy(); }
?>

1 个答案:

答案 0 :(得分:1)

这可能令人窒息:

mysqli_query($con,"UPDATE users WHERE Username='$_SESSION[user]' SET Username=$newingamename");
                                                1 ^^^^^^^^^^^^^               2 ^^^^^^^^^^^^

在(1)中,当你想要的是$_SESSION[user]时($_SESSION['user']附近有引号),这相当于说user

在(2)中,你需要在$ newingamename周围加上单引号。

注意:其余内容是根据以下原始答案进行编辑的:(a)合并PHP和MySQL修复程序,以及(b)使用mysqli参数绑定重新构建代码安全。

此外,MySQL查询错误:SET出现在WHERE之前。

将所有这些放在一起,使用mysqli绑定参数进行代码(和职业)安全性,你得到这个:

$stmt = mysqli_prepare($con, 'UPDATE users SET username=? WHERE username = ?');
mysqli_stmt_bind_param($stmt, 'ss', $newingamename, $_SESSION['user']);
mysqli_stmt_execute($stmt);

对其他UPDATE进行类似的更改。