dongpan1308 2014-01-22 18:50
浏览 74
已采纳

更改数据库中的用户名

<?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>';
    ?>

I have the following code. I'm trying to make it so when one completes the 'newuser' field, and presses the 'Change' button, his username (username is written in $_SESSION['usr']) gets changed to the user in the 'newuser' field. But it keeps failing, further more I have the following error:

Catchable fatal error: Object of class stdClass could not be converted to string in /home/u594115708/public_html/edituser.html on line 129

  • 写回答

3条回答 默认 最新

  • douxian9010 2014-01-22 18:57
    关注

    Change this line:

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

    To this:

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

    Also add at the begin of the php script:

    session_start();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度