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!
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:
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.
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.
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:
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:
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.
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..