Question

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

Was it helpful?

Solution 2

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();

OTHER TIPS

$_SESSION['usr'] is an object and not a scalar variable. The value you are looking for is inside that object. I am guessing it can be reached by using:

$_SESSION['usr']->user;

So that line of code should look like this:

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

$usr is not being overwritten because your $_SESSION variables are not accessible, since you did not call session_start() at the beginning of your script.

Add session_start() to the beginning, like so:

<?php

session_start();

...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top