Code is either looping or ignoring my If's[fixed]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Code is either looping or ignoring my If's[fixed]

Post by SirChick »

Hey guys i have a input system for messages but it sends two of the same message yet with different data...for "Reciever" (in other words the person it sends to).

Say i send it to username "Dave" which is ID 47

the Database shows 2 of the same message one with "47" then the other has "0". Very odd... its either looping but my server would time out and the fact is only does 2 makes me thing loop is very unlikely, or the most likely option is cos i have so many if statements my structure is a bit problematic logically.. can any one explain this? Code is provided below:

Code: Select all

If (isset($_POST['SendInputLetter']))
{
$Selection = $_POST['UserID'];
If ($Selection == 1 ) 
			{
//collect the data needed
				$Sender = $_SESSION['Current_User'];
				$MessageText = mysql_real_escape_string($_POST['Letter']);
				$Username = mysql_real_escape_string($_POST['Username']);
				$Date = date("Y-m-d H:i:s",time());
				$Subject = mysql_real_escape_string($_POST['Subject']);
//check if user exists
$query = "SELECT * FROM userregistration WHERE Username='$Username'";
$GetUserName = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);
						if (!($row = mysql_fetch_assoc($GetUserName))) 
						{
							die('This Username does not exist!');
						}
				
						$UserID = $row["UserID"];					
				
//stop user sending messages to them self encase they are that stupid or lonely
						If ($UserID == $_SESSION['Current_User'])
						{
						die('You cannot send messages to yourself');
						}
					Else
						{
//insert into the database yay!
$query = "INSERT INTO `messages` (Reciever, Sender, Senttime, MessageText, Subject)
Values ('$UserID', '$Sender', '$Date', '$MessageText', '$Subject')";
mysql_query($query) or die(mysql_error(). " with query ". $query); // get useful error message
		
						}
			}

		If ($Selection == 2 ) 
			{
//collect relevant data
				$Sender = $_SESSION['Current_User'];
				$UserID = mysql_real_escape_string($_POST['UserIDInput']);
				$Date = date("Y-m-d H:i:s",time());
				$Subject = mysql_real_escape_string($_POST['Subject']);
				$MessageText = mysql_real_escape_string($_POST['Letter']);

$CheckUserID = mysql_query("SELECT * FROM userregistration WHERE UserID='$UserID'") or die(mysql_error());
//check user exists			
		If (!($row = mysql_fetch_assoc($CheckUserID))) 
		{
		die('This Username does not exist!');
		}
		
									
//preventing idiots messaging them selfs and report bugs about it
	If ($UserID == $Sender)
	{
	die('You cannot send messages to yourself!');
	}
	Else 
	{
//insert into database yay

$secondquery = "INSERT INTO `messages` (Reciever, Sender, Senttime, MessageText, Subject)
Values ('$UserID', '$Sender', '$Date', '$MessageText', '$Subject')";
mysql_query($secondquery) or die(mysql_error(). " with query ". $secondquery); // get useful error message
		}
						
	
	 }

ElseIf ($Selection == 0 ) 
		{
			die('Please click the selection of Username OR User ID');
		}

}
Last edited by SirChick on Tue Aug 28, 2007 6:22 pm, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Have you tried debugging?

IE: die('im inside the loop'); inside of the loop or if/statement you're expecting the code to be in?
Turned on error reporting?
var_dump()'d some variables?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

well what i done was seperated the code firstly...and i echo'd the variables... no die's occur cos is successfully does the script with out an error.


However as u can see i have a big if statement which then the rest of the script is inside which is:


$Selection = $_POST['UserID'];
If ($Selection == 1 )
{code}
If ($Selection == 2 )
{code}



these are the 2 that its both doing.. which it surely should not be. The selection is from radio buttons:

Code: Select all

<input type="radio" id="RadioButton1" name="UserID" value="1" style="position:absolute;left:177px;top:582px;z-index:14">
<input type="radio" id="RadioButton2" name="UserID" value="2" style="position:absolute;left:179px;top:662px;z-index:15">

thats why i dont get it.... ive tried the option of doing radiobutton value 2 and it works but then radiobutton value 1 does both the if statements :S
Steve Mellor
Forum Commoner
Posts: 49
Joined: Thu Aug 02, 2007 8:18 am

Post by Steve Mellor »

You've not got any loops in your code and that makes me think that there is some sort of problem passing variables to the page perhaps. Now, is this the case that it only happens when you select number one?

How about trying it with 'elseif' as oposed to 'if' for the second statement. There's no reason why that should work but then again I can see no reason why it wouldn't. That would make your main condition something like this:

Code: Select all

$Selection = $_POST['UserID'];
If ($Selection == 1 )
  {code}
elseIf ($Selection == 2 )
  {code}
Also, you are sending your user ID along in the $selection variable. Is there any reason why in the first condition ( $Selection == 1 ) you are testing against the username instead of the user ID?

It's probably something I'm missing to be honest but I thought that the statement:

Code: Select all

$query = "SELECT * FROM userregistration WHERE Username='$Username'";
could cause a problem if you have more than one user with the same username (whether intentional or not).
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

If ($Selection == 1 )

is if the user wants to send a letter but using username to say who to send to


If ($Selection == 2)
is if they want to send a letter via ID

i managed to get it working by the way.

I did what you said by putting the if else statement instead of If..



though its fixed it .. is there a reason why that happens cos surely the "if" should have still worked at the time?
Steve Mellor
Forum Commoner
Posts: 49
Joined: Thu Aug 02, 2007 8:18 am

Post by Steve Mellor »

I don't actually know why it would have happened. It just made sense to me that if a condition was being ignored and two actions were being performed then enclosing them into the same statement with 'elseif' should stop it happening. The way 'elseif' works means that the second condition won't be tested unless the first is untrue where as the way you had it before would test both conditions whether the one was found to be true or not.
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

yeh. yet the second If should have still been ignore anyway cos that was also untrue
Steve Mellor
Forum Commoner
Posts: 49
Joined: Thu Aug 02, 2007 8:18 am

Post by Steve Mellor »

Yep, go figure. This sort of thing seams to happen all the time though. There's probably a perfectly rational explanation. No idea what it is though :wink:
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

sorry to bump this but its not working again. Upon page the load the script runs :S it should only do it when isset is true... so its still ignoring the if's..

gah its fixed now ;)
Post Reply