I have an update script that updates username, password, telephone,usergenerated id etc. So lets say a user decides to change his/her telephone but not the username. But when they go to edit their information, all of the above mentioned fields are present on the form. So the also get submitted along with what the user wants to have changed. So i am using the code below to detect that. So if the username in the field matches the username pulled from the session, it means its ok to continue, if not then print that the username is already taken. But the issue is that i have to run this query twice, once for the username and once for the unique id by the user. Further it gets complicated when the Update script is at the bottomo of the page. The codes below will detect it but they would stop at echo. So how do i make it so the code checks for those condi开发者_开发问答tions and then upon successful completion it points to the update query.
Check 1:
if ($username == $_POST['username') {
echo "Good to go";
} else {
echo "already in use";
}
Check 2:
if ($usergenid == $_POST['usergenid') {
echo "Good to go";
} else {
echo "already in use";
}
maybe i'm misunderstanding the question, but can you nest your conditions?
Original
$result = null;
if($username == $_POST['username'])
{
if($usergenid == $_POST['usergenid'])
{
/* perform update to user */
$sql = 'UPDATE users SET username = ' . $_POST['username'] . ' WHERE id = ' . $_POST['usergenid'];
if(mysql_query($sql))
{
$result = 'User updated';
}
else
{
$result = 'Update failed';
}
}
else
{
$result = 'User id already in use';
}
}
else
{
$result = 'Username already in use';
}
return $result;
Update username if user_id matches
$result = null;
if($usergenid == $_POST['usergenid'])
{
if($username != $_POST['username'])
{
/* perform update to user */
$sql = 'UPDATE users SET username = ' . $_POST['username'] . ' WHERE id = ' . $_POST['usergenid'];
if(mysql_query($sql))
{
$result = 'username updated';
}
else
{
$result = 'update failed';
}
}
else
{
$result = 'username is the same, no update performed';
}
}
else
{
$result = 'user id does not match';
}
return $result;
精彩评论