• 0

Trouble with PHP if and submitting a form


Question

So as the title say, two problems here...

First is about a if statement that behaves well not like I expect it, maybe i did something wrong but if so I can't see it.

The other is that when I send the form I have in the page it doesn't send the browser to the page specified in action like it should, this isn't the first form I've done and I've never had these errors.

Ill just post the code in question and go on after that... If anyone would want it in plain HTML, what you get when viewing the page source in a browser just ask, I've checked the code that way and everything is right as far as I see...

Code might be a little rough, just threw it together quite quickly last night, wont clean it and make it nice until everything works.

<?php session_start();
include("connect.inc");

$QStr = $_GET['user'];

$user=mysql_fetch_array(mysql_query("select * from userlist where username='". $QStr ."'"));

if(isset($_SESSION['username']))
{  

/*if($_SESSION['usertype']!="admin"||$_SESSION['usertype']!="moderator")
{
		if($QStr!=$_SESSION['username'])
		{
			mysql_close($con);
			header('location: index.php');
		}
	}
}*/

	if($_SESSION['usertype']!="admin")
	{
		if($_SESSION['usertype']!="moderator")
		{
			if($QStr!=$_SESSION['username'])
			{
				mysql_close($con);
				header('location: index.php');
			}
		}
	}
	if($_SESSION['usertype']=="moderator")
	{
		if($user['usertype']=="admin")
		{
			mysql_close($con);
			header('location: index.php');
		}
	}

}
else
{
	mysql_close($con);
	header('location: index.php');
}

echo 	"<html>
		<head>
		<link rel=\"stylesheet\" type=\"text/css\" title=\"standard\" href=\"css.css\"/>
		<style type=\"text/css\"> body { margin-top:10px; margin-left:60px; margin-right:60px;}
		</style>
		</head>
		<body>";

if($_SESSION['usertype']=="admin"||$_SESSION['usertype']=="moderator")
{
	echo	"Admintools liked remove and block will be here"
}

echo	"<form>
		<form action=\"updateuser.php\" method=\"post\">
		<input type=\"hidden\" name=\"id\" value=\"". $user['ID'] ."\">";

if($_SESSION['usertype']=="admin")
{	
	echo	"Anv?ndarnamn: <input type=\"text\" name=\"username\" value=\"". $user['username'] ."\" style='width:150px;'/><br/>";

}
if($_SESSION['usertype']!="admin")
{
	echo 	"Anv?ndarnamn: <input type=\"text\" name=\"username\" value=\"". $user['username'] ."\" readonly><br/>";
}
echo	"F?rnamn: <input type=\"text\" name=\"name\" value=\"". $user['name'] ."\" style='width:150px;'/><br/>
		L?senord: <input type=\"text\" name=\"password\" value=\"". $user['password'] ."\" style='width:150px;'/><br/>
		Email: <input type=\"text\" name=\"email\" value=\"". $user['email'] ."\" style='width:150px;'/><br/>";

if($_SESSION['usertype']=="admin"||$_SESSION['usertype']=="moderator")
{		
	echo	"Anv?ndartyp: <input type=\"text\" name=\"usertype\" value=\"". $user['usertype'] ."\" style='width:150px;'/><br/>";
}
else{
echo "<input type=\"hidden\" name=\"usertype\" value=\"". $user['usertype'] ."\"><br/>";
}

echo	"<input type=\"submit\" name=\"submit\" value=\"Spara\">
		<input type=\"button\" name=\"cancel\" value=\"Avbryt\" onclick=Javascript:history.go(-1)>
		</form>
		</body>
		</html>";
?>

OK, so you can probably see the if statement, it's early in the code and commented out, the problem is that even if $_SESSION['usertype'] is admin or moderator it still executes the code, if I remove one of them and just checks for one of them it works, I've done if statements with more than one condition before and it worked, can't it be done when checking if something is not equal or what? What am I missing here? As you might see in the next if statement in there it works if I separate them but don't like to do it like that...

And the second problem, the form, when I click the submit button the browser should go on to "updateuser.php" but it wont, it just stays at the same page and ads a lot of querystrings to the URL, for example "/edituser.php?id=4&username=sergh&name=fgbfg&password=bggh&email=mail%40test.com&usertype=standard&submit=Spara"

So anyone who can help me figure this out?

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

if($_SESSION['usertype']!="admin"||$_SESSION['usertype']!="moderator")
{

try instead:

if($_SESSION['usertype'] !="admin" && $_SESSION['usertype']!="moderator")
{

echo    "<form>
                <form action=\"updateuser.php\" method=\"post\">

You have the form tag twice. Remove the first one, then it should work.

Link to comment
Share on other sites

  • 0

im a bit rusty on php but i think you can do this

if(isset($_SESSION['usertype']) && $_SESSION['usertype'] != "moderator") { echo "do this"; }

this will execute code if "usertype" equals anything but moderator

replace ! with = to execute the code when "usertype" equals moderator

Link to comment
Share on other sites

  • 0

try instead:

if($_SESSION['usertype'] !="admin" && $_SESSION['usertype']!="moderator"){

You have the form tag twice. Remove the first one, then it should work.

Thanks, you solved both problems :) how stupid of me of course it should be if its not admin and if its not moderator and not if its not or if its not like wrote :p

Used an old if statement I have in other places but in those places it has to be one of them so yea :p missed some thinking when editing that :p

And thanks for finding that second form, didn't see it :p

im a bit rusty on php but i think you can do this

if(isset($_SESSION['usertype']) && $_SESSION['usertype'] != "moderator") { echo "do this"; }

this will execute code if "usertype" equals anything but moderatorreplace ! with = to execute the code when "usertype" equals moderator

that wouldn't have helped a lot since I need it to execute if it anything else than admin or moderator, either of those and it should not run, but thanks anyway :)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.