Code is either looping or ignoring my If's[fixed]
Posted: Mon Aug 27, 2007 6:52 pm
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:
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');
}
}