Copy link to clipboard
Copied
I am having trouble with this code. If the user submits the form, it always returns that the account has been updated.
if(isset($_POST['changeUname'])) {
$newUname = $_POST['new_uname'];
if($newUname != "" && $newUname != null) {
$query = "SELECT uname, change_uname FROM members WHERE ID='$userID'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result) or die(mysql_error());
if($row['change_uname'] == 0) {
mysql_query("UPDATE members SET uname = '$newUname', change_uname = '1' WHERE ID = '$ID'");
} else {
$changeUnameErrors[] = "You cannot change your username. You have already changed it.";
}
}
} else {
$changeUnameErrors[] = "Please enter a valid username!";
}
if (mysql_affected_rows() > 0) {
echo "<p><span style='color:red; font-weight:strong; font-size:12px;'>Your account has been successfully updated!</span><p>";
}
}
Copy link to clipboard
Copied
Troubleshoot by echoing the value of $row['change_uname'] and mysql_affected_rows() in the appropriate spot. One of these is most likely not what you are expecting.
Copy link to clipboard
Copied
mysql_affected_rows() = 1.... how can this be? nothing was changed....
well anyway, i guess this is one way to fix it....
$query = "SELECT uname FROM members WHERE ID='$userID' AND change_uname='0'"; (0 being false)
tacked on this else statement if that query fails:
else {
$changeUnameErrors[] = "Please enter a valid username!";
}
Copy link to clipboard
Copied
>mysql_affected_rows() = 1.... how can this be? nothing was changed....
We can't answer that without knowing what the existing data in the database is, and what the data submitted from the form is.
>well anyway, i guess this is one way to fix it....
Sure, but it's always good to know why it's not working the way you think it should.
Copy link to clipboard
Copied
OK, here you go.
>if($row['change_uname'] == 0) {
You are testing if the value is equal to zero
>UPDATE members SET uname = '$newUname', change_uname = '1' WHERE ID = '$ID'"
You are updating that column to a text value. This is why it is so important to datatype correctly.
Copy link to clipboard
Copied
so i messed around with the code, and it will do the same thing. when you submit an empty field, it says that the account was updated. when you put something in, it doesn't give off that message... really weird!!
<?php
if(isset($_POST['changeUname'])) {
if($newUname != "" && $newUname != null) {
$query = "SELECT uname FROM members WHERE ID='$userID' AND change_uname='0'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result) or die(mysql_error());
if($newUname != $row['uname']) {
mysql_query("UPDATE members SET uname = '$newUname', change_uname = '1' WHERE ID = '$ID'");
} else {
$changeUnameErrors[] = "You must type in a different username than what you already have!";
}
} else {
$changeUnameErrors[] = "You have already changed your username!";
}
} else {
$changeUnameErrors[] = "You must type in a username!";
}
echo mysql_affected_rows() . "whoa!";
if (mysql_affected_rows() > 0) {
// echo mysql_affected_rows();
echo "<p><span style='color:red; font-weight:strong; font-size:12px;'>Your account has been successfully updated!</span><p>";
}
if(isset($changeUnameErrors)) {
echo "<strong>The following errors have occured: </strong>";
for($i=0; $i<count($changeUnameErrors); $i++) {
echo "<br> -$changeUnameErrors[$i]";
}
}
} else {
$changeUnameErrors[] = "Please enter a valid username!";
}
?>
Copy link to clipboard
Copied
>if(isset($_POST['changeUname'])) {
Here you are testing the value of the form field.
>if($newUname != "" && $newUname != null) {
What is the value of $newUname? You haven't assiged it anywhere in the code you posted.
>change_uname = '1'
Not your current problem anymore, but if change_uname contains only numeric values, why not make it a numeric field? Much less error prone that way.
Copy link to clipboard
Copied
oh, it should actually be:
if(isset($_POST['changeUname'])) {
$newUname = $_POST['new_uname'];
also changed to this: $query = "SELECT uname FROM members WHERE ID='$userID' AND change_uname=0";
also did this for the second query.
Still doesn't work though.
just though of something...
if i have this above this snippet of code, could it affect the "results"? would it be possibly pulling the 1 from this snippet of code?
<?php
$ID = $_SESSION['testSite'];
$query = "SELECT lastLoginDate FROM members WHERE ID='$ID'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result) or die(mysql_error());
$lastLoginDate = $row['lastLoginDate'];
}
if(isset($_POST['update_acnt_prefs'])) {
$update_pword = $_POST['update_pword'];
$update_pin = $_POST['update_pin'];
$set_new_pin = $_POST['set_new_pin'];
if(isset($update_pin)) {
if(empty($set_new_pin)) {
$invalidPin = 1;
}
}
if($invalidPin != 1) {
$query = "SELECT askPword, pin FROM members WHERE ID='$ID'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result) or die(mysql_error());
mysql_query("UPDATE members SET askPword = '$update_pword', pin = '$set_new_pin' WHERE ID = '$ID'");
}
if(mysql_affected_rows() > 0) {
$update_success = true;
}
}
}
?>
it seems to give me a -1 value when i remove this, but the weird thing is, it shouldn't be running this anyway, since we are not doing anything with this other form....
Copy link to clipboard
Copied
What is the datatype of 'change_uname'
>if i have this above this snippet of code, could it affect the "results"?
Which code above what code?
Copy link to clipboard
Copied
1) it is either a 1 or a 0; if it is a 0, then the username has not been changed yet.
2) basically, i have this:
<?php
$ID = $_SESSION['testSite'];
$query = "SELECT lastLoginDate FROM members WHERE ID='$ID'";
$result = mysql_query($query) or die(mysql_error());
// this is where the 1 begins
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result) or die(mysql_error());
$lastLoginDate = $row['lastLoginDate'];
}
?>
From the code, when i return the number of affected rows at this point, i get a 1. so then i went down to where I am updating the username. the first thing i do there, is echo the number of affected results, which comes out to be one. That is probably why it will return the "account update successfull" message even if it does not change anything at all.
Copy link to clipboard
Copied
>1) it is either a 1 or a 0; if it is a 0, then the username has not been changed yet.
No, I mean what is the datatype of the column change_uname ?
Copy link to clipboard
Copied
int(1)