So I am working on an admin
panel feature in PHP 5.6
and whenever I try to switch a user to Admin it will set them to Builder I have no idea where or what I am doing wrong Here is the code
this is updateusera.php
<?php include 'core/init.php';
$id = $_GET['id'];
$type = $_GET['type'];
if($type == 'admin'){
mysql_query("UPDATE `users` SET `type` = 'user' WHERE `user_id` = '$id'");
header('location: changeusers.php');
} else if($type =='user'){
mysql_query("UPDATE `users` SET `type` = 'admin' WHERE `user_id` = '$id'");
header('location: changeusers.php');
}
//=========================================================================
if($type == 'moderator'){
mysql_query("UPDATE `users` SET `type` = 'user' WHERE `user_id` = '$id'");
header('location: changeusers.php');
} else if($type =='user'){
mysql_query("UPDATE `users` SET `type` = 'moderator' WHERE `user_id` = '$id'");
header('location: changeusers.php');
}
//=========================================================================
if($type == 'builder'){
mysql_query("UPDATE `users` SET `type` = 'user' WHERE `user_id` = '$id'");
header('location: changeusers.php');
} else if($type =='user'){
mysql_query("UPDATE `users` SET `type` = 'builder' WHERE `user_id` = '$id'");
header('location: changeusers.php');
}
?>
this is the changeusers.php file
for some reason it would not let me put both of my codes in here P.S I am new to Stackoverflow so i'm still getting use to all this
if anyone has an idea I would love to hear it
Thanks!
You have three else if
statements which is true
when $type =='user'
. Everything in these else if
executing and three queries send, last of them set type builder.
You need to pass in parameters type which you need to set. That way:
if($type == 'admin') {
echo "<a href='updateusera.php?id=&type=user'>Revoke Admin </a>";
} else {
echo "<a href='updateusera.php?id=$id&type=admin'>Grant Admin </a>";
}
if($type == 'moderator') {
echo "<a href='updateusera.php?id=$id&type=user'>Revoke Moderator </a>";
} else {
echo "<a href='updateusera.php?id=$id&type=moderator'>Grant Moderator </a>";
}
if($type == 'builder') {
echo "<a href='updateusera.php?id=$id&type=user'>Revoke Builder</a>";
} else {
echo "<a href='updateusera.php?id=$id&type=builder'>Grant Builder</a>";
}
Then you code need to be like that:
if($type == 'user') {
mysql_query("UPDATE `users` SET `type` = 'user' WHERE `user_id` = '$id'");
header('location: changeusers.php');
exit;
}
if($type == 'admin') {
mysql_query("UPDATE `users` SET `type` = 'admin' WHERE `user_id` = '$id'");
header('location: changeusers.php');
exit;
}
if($type == 'builder') {
mysql_query("UPDATE `users` SET `type` = 'builder' WHERE `user_id` = '$id'");
header('location: changeusers.php');
exit;
}
You want to change your else if
statements to if statements; Basically all the else if
statements are being executed and the last else if
statement is set to builder.