更改数据库中的用户名

时间:2014-01-22 18:50:37

标签: php mysql database account

<?php
    if($_SESSION['id']) {
                $result = mysql_query("
    SELECT
      id,usr,pass,email,dt,priv
    FROM
      tz_members
    WHERE
      usr = '" . $_SESSION['usr'] . "'
");
$done=0;
                while($row = mysql_fetch_array($result))
                {
                echo '<div class="roundbox"><div class="grey"><h2>Change Username</h2><br>Below you can view your current informations.<br>This page allows you to change your username!<br><table>';
                echo "<tr><td><strong>Unique ID:</strong></td><td>" . $row['id'] . "</td><em><td>(Can't be seen by others!)</em></td></tr>";
                echo "<tr><td><strong>Username:</strong></td><td>" . $row['usr'] . "</td><em><td>(Can be seen by others!)</em></td></tr></table><br>";
                echo '<form action="edituser.html" method="post">
                        <div class="grey">New Username:</div><br>
                        <input type="text" name="newuser" size="20" maxlength="40" value=""><br><br>
                        <input type="submit" name="submit" value="Change"></div>
                      </form></table>'; }
if($_POST['submit']=='Change') {
$usr = $_SESSION['usr'];
$query="SELECT usr FROM tz_members WHERE usr = '$usr'";
$st=mysql_query($query);
$recs=mysql_num_rows($st);
$row=mysql_fetch_object($st);
$newuser = $_POST['newuser'];
mysql_query("UPDATE tz_members 
SET 
$row->$usr 
WHERE 
usr = $usr");
$done=1;
}

if($done == 1)
Header("Location: login_panel/userchanged.html");
}
    else echo '<div class="roundbox"><h2>You must be <a href="login.html">logged in</a> to change your username!</h2></div>';
    ?>

我有以下代码。我试图这样做,当一个人完成'newuser'字段,并按下'更改'按钮,他的用户名(用户名写在$ _SESSION ['usr'])被改为'newuser'中的用户领域。但它仍然失败,更多我有以下错误:

  

捕获致命错误:第129行/home/u594115708/public_html/edituser.html无法将类stdClass的对象转换为字符串

3 个答案:

答案 0 :(得分:1)

$_SESSION['usr']是一个对象而不是标量变量。您要查找的值是在该对象内。我猜测可以通过使用:

来达到
$_SESSION['usr']->user;

所以代码行应如下所示:

$usr = $_SESSION['usr']->user;

答案 1 :(得分:0)

$usr未被覆盖,因为您的$_SESSION变量无法访问,因为您未在脚本开头调用session_start()

session_start()添加到开头,如下所示:

<?php

session_start();

...

答案 2 :(得分:0)

更改此行:

mysql_query("UPDATE tz_members SET $row->$usr WHERE usr = $usr");

对此:

mysql_query("UPDATE tz_members SET usr = '{$newusr}' WHERE usr = '{$usr}'");

还在php脚本的开头添加:

session_start();