PDA

View Full Version : choosing a field within a row.


Zurke
03-01-2005, 08:01 PM
PHP 5.0.2
MySQL 4.1.7
Apache 1.3.33

Hi all,

I am allowing my members to change their usernames after registering. I would prefer that if they do change their usernames for use within the forums that their new name is displayed within my members page.

I have blindly tried some "if" statements but cant seem to get anything to work. Here is something I tried but it was fruitless. Any Ideas?

"newname is the name they could change to from username after registering"

$query='SELECT newname, username, createdate FROM chmembers';
$result = mysqli_query($connection,$query);
if (!$result){
exit('unable to retrieve table mysql_error())';
}

while ($row = mysqli_fetch_array($result)){

$newname=$row[newname];
$createdate=$row[createdate];

"if ($row[newname]==0) {$newname = $row[username]};"

}
mysqli_close ($connection);

<body>
<table>$newname</table>

chump2877
03-02-2005, 01:02 AM
If all you want to do is change 'username' to 'newname', and assuming that 'newname' is a POST variable and 'username' is probably (or should be) a SESSION variable, try something like this:

// start the session
session_start();


$newname = $_POST['newname'];


$dbcon = mysql_connect("localhost","username","password");
mysql_select_db("MyDatabase",$dbcon);

$query = "UPDATE chmembers SET username='". $newname. "' WHERE username LIKE '%".$_SESSION['username']."%'";
$result = mysql_query($query);
if (mysql_errno())
{
die("<br>" . mysql_errno() . ": " . mysql_error() . "<br>");
}
if (mysql_affected_rows() < 1)
{
die("<br>Failed to add user information.");
}

Zurke
03-02-2005, 03:34 PM
Thanks chump2877,

But no it has nothing to do with sessions or a form. I do not want certain characters used when new members register.
But I want them to be able to use an alias with anything they want after they have registered. I give that option in profile.

Now, when all members view the "members list", IF someone has opted for an alias ( I like them myself), I want to list the members alias instead of the registered username. That would require the "members page" script to opt for the alias(if there is one) instead of the registered username when retrieving from the table.

Gee, I almost confused myself writing it. But it is plain and seems simple but I just cant seem to work it out.

delinear
03-02-2005, 04:59 PM
So you just want to test if the newname field is empty and if it is you want to use the username instead? Try this...

while ($row = mysqli_fetch_array($result)){
$newname = (!empty($row['newname']) ? $row['newname'] : $row['username']);
$createdate=$row[createdate];
}

Zurke
03-02-2005, 06:24 PM
delinear,

Bingo! Thanks it works like a charm. That is some code I did come close to trying. I was using "if" statements that were not working. But I still am in the noob stage of php. Thanks hugely.