Leveecius Posted November 17, 2009 Share Posted November 17, 2009 I have a quick question about a query I'm trying to figure out. I'm trying to do something in my game that i can't seem to get down. What I am trying to do is make it so that when a person in my game makes enough rankpoints to advance it does one of 2 different things. The first thing it does is it checks to see if you are in a crew. If you are in a crew it sends a message to your crew staff and informs them that you are ready to be ranked (basically getting rid of automatic ranking). so I know I use the if and else command, but here is what I have and it's not working. it keeps telling me I have been promoted and it's not checking for my crew. Can you check to see if there is a problem with my coding? Here is the coding if (!$done){ $done="0"; } if ($done == "1"){ $crewchk = mysql_query("SELECT crew FROM users WHERE username='$username'"); $crewboss = mysql_query("SELECT owner FROM crews WHERE owner='$owner'"); mysql_query("INSERT INTO `inbox` ( `id` , `to` , `from` , `message` , `date` , `read` , `saved` , `event_id` ) VALUES ( '', '$crewboss', '$username', 'You have a member ready to be promoted to $newrank! Let him rank?', '$date', '0', '0', '0' )"); }elseif ($crewchk == "0"); mysql_query("UPDATE users SET rank='$newrank' WHERE username='$username'"); mysql_query("INSERT INTO `inbox` ( `id` , `to` , `from` , `message` , `date` , `read` , `saved` , `event_id` ) VALUES ( '', '$username', '$username', 'You have been promoted to $newrank your doing well!', '$date', '0', '0', '0' )"); }[code] Quote Link to comment https://forums.phpfreaks.com/topic/181910-query-question/ Share on other sites More sharing options...
mikesta707 Posted November 17, 2009 Share Posted November 17, 2009 thats because you have the elseif connected to the if. if you want both to be able to fun, change the elseif to an if. the elseif won't run if the if before it runs (or any other elseifs before it have run) Quote Link to comment https://forums.phpfreaks.com/topic/181910-query-question/#findComment-959458 Share on other sites More sharing options...
Psycho Posted November 17, 2009 Share Posted November 17, 2009 I see a few potential problems: $crewchk = mysql_query("SELECT crew FROM users WHERE username='$username'"); $crewboss = mysql_query("SELECT owner FROM crews WHERE owner='$owner'"); You run those two queries and assign the results to those two variable. THEN you try and use those two variable in an INSERT query. Those variables are not values, they are resource IDs to the result fo the queries. You need to extract the values. mysql_query("INSERT INTO `inbox` ( `id` , `to` , `from` , `message` , `date` , `read` , `saved` , `event_id` ) VALUES ( '', '$crewboss', '$username', 'You have a member ready to be promoted to $newrank! Let him rank?', '$date', '0', '0', '0' )"); Why include `id` in the INSERT if you are not going to provide a value? }elseif ($crewchk == "0"); mysql_query("UPDATE users SET rank='$newrank' WHERE username='$username'"); Not even sure what that is supposed to do. You have a semicolon after the elseif clause so the elseif ends there and all code after it will be run without regard to the else if. You should format your code with some structure and add error handling/checking to find errors easier. The example below uses an "or die()" on the queries. It is fine for quick validations but shoudl not be included in your final code. I prefer to build my own query error handler to prevent error messages going to the user with technical data. <?php if (!$done) { $done="0"; } if ($done == "1") { $query = "SELECT crew FROM users WHERE username='$username'"; $result = mysql_query($query) or die ("Error:<br>".mysql_error()."<br><br>Query:<br>$query"); $crewchk = mysql_result($result, 0, 'crew'); $query = "SELECT owner FROM crews WHERE owner='$owner'"; $result = mysql_query($query) or die ("Error:<br>".mysql_error()."<br><br>Query:<br>$query"); $crewboss = mysql_result($result, 0, 'crew'); $query = "INSERT INTO `inbox` (`to`, `from`, `message`, `date`, `read`, `saved`, `event_id`) VALUES ('$crewboss', '$username', 'You have a member ready to be promoted to $newrank! Let him rank?', '$date', '0', '0', '0')"; $result = mysql_query($query) or die ("Error:<br>".mysql_error()."<br><br>Query:<br>$query"); } elseif ($crewchk == "0") { $query = "UPDATE users SET rank='$newrank' WHERE username='$username'"; $result = mysql_query($query) or die ("Error:<br>".mysql_error()."<br><br>Query:<br>$query"); $query = "INSERT INTO `inbox` (`to`, `from`, `message`, `date`, `read`, `saved`, `event_id`) VALUES ('$username', '$username', 'You have been promoted to $newrank your doing well!', '$date', '0', '0', '0')"; $result = mysql_query($query) or die ("Error:<br>".mysql_error()."<br><br>Query:<br>$query"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/181910-query-question/#findComment-959469 Share on other sites More sharing options...
Leveecius Posted November 17, 2009 Author Share Posted November 17, 2009 :-\ Ok now I'm getting an unexpected $end in line 333, to which I know means there is something not ended right, but I can't see it. Here is the full rank function to my game. See if there is something you can see that I can't. function rankcheck(){ $username=$_SESSION['username']; $query=mysql_query("SELECT * FROM users WHERE username='$username' LIMIT 1"); $info = mysql_fetch_object($query); $date = gmdate('Y-m-d h:i:s'); if ($info->rank == "High School Punk" && $info->rankpoints >= "400"){ $newrank="School Bully"; $done="1"; } elseif ($info->rank == "School Bully" && $info->rankpoints >= "12300"){ $newrank="Package Boy"; $done="1"; } elseif ($info->rank == "Package Boy" && $info->rankpoints >= "2000"){ $newrank="Street Runner"; $done="1"; } elseif ($info->rank == "Street Runner" && $info->rankpoints >= "4320"){ $newrank="Soldier"; $done="1"; } elseif ($info->rank == "Soldier" && $info->rankpoints >= "6258"){ $newrank="Criminal"; $done="1"; } elseif ($info->rank == "Criminal" && $info->rankpoints >= "9600"){ $newrank="Apprentice"; $done="1"; } elseif ($info->rank == "Apprentice" && $info->rankpoints >= "15073"){ $newrank="Collector"; $done="1"; } elseif ($info->rank == "Collector" && $info->rankpoints >= "22512"){ $newrank="Assassin"; $done="1"; } elseif ($info->rank == "Assassin" && $info->rankpoints >= "34005"){ $newrank="Known Assassin"; $done="1"; } elseif ($info->rank == "Known Assassin" && $info->rankpoints >= "65511"){ $newrank="Racket Boss"; $done="1"; } elseif ($info->rank == "Racket Boss" && $info->rankpoints >= "80106"){ $newrank="Advisor"; $done="1"; } elseif ($info->rank == "Advisor" && $info->rankpoints >= "110020"){ $newrank="Trusted Advisor"; $done="1"; } elseif ($info->rank == "Trusted Advisor" && $info->rankpoints >= "140109"){ $newrank="Respected Criminal"; $done="1"; } elseif ($info->rank == "Respected Criminal" && $info->rankpoints >= "180000"){ $newrank="Mobster"; $done="1"; } elseif ($info->rank == "Mobster" && $info->rankpoints >= "240133"){ $newrank="Feared Mobster"; $done="1"; } elseif ($info->rank == "Feared Mobster" && $info->rankpoints >= "300602"){ $newrank="Regional Leader"; $done="1"; } elseif ($info->rank == "Regional Leader" && $info->rankpoints >= "500000"){ $newrank="Consligere"; $done="1"; } elseif ($info->rank == "Consligere" && $info->rankpoints >= "750000"){ $newrank="Godfather"; $done="1"; } elseif ($info->rank == "Godfather" && $info->rankpoints >= "1250000"){ $newrank="Feared Godfather"; $done="1"; } elseif ($info->rank == "Feared Godfather" && $info->rankpoints >= "1500000"){ $newrank="Don"; $done="1"; } elseif ($info->rank == "Don" && $info->rankpoints >= "2000000"){ $newrank="Respected Don"; $done="1"; } elseif ($info->rank == "Respected Don" && $info->rankpoints >= "2250000"){ $newrank="Underboss"; $done="1"; } elseif ($info->rank == "Underboss" && $info->rankpoints >= "2500000"){ $newrank="Boss"; $done="1"; } elseif ($info->rank == "Boss" && $info->rankpoints >= "3000000"){ $newrank="Regional Head"; $done="1"; } elseif ($info->rank == "Regional Head" && $info->rankpoints >= "3500000"){ $newrank="International Head"; $done="1"; } elseif ($info->rank == "International Head" && $info->rankpoints >= "4000000"){ $newrank="Right Hand Man"; $done="1"; } elseif ($info->rank == "Right Hand Man" && $info->rankpoints >= "5000000"){ $newrank="Family Head"; $done="1"; } elseif ($info->rank == "Family Head" && $info->rankpoints >= "7500000"){ $newrank="Syndicate Underboss"; $done="1"; } elseif ($info->rank == "Syndicate Underboss" && $info->rankpoints >= "10000000"){ $newrank="[b]Syndicate Boss[/b]"; $done="0"; } if (!$done){ $done="0"; } if ($done == "1"){ $crewchk = mysql_query("SELECT crew FROM users WHERE username='$username'"); $crewboss = mysql_query("SELECT owner FROM crews WHERE owner='$owner'"); mysql_query("INSERT INTO `inbox` ( `id` , `to` , `from` , `message` , `date` , `read` , `saved` , `event_id` ) VALUES ( '', '$crewboss', '$username', 'You have a member ready to be promoted to $newrank! Let him rank?', '$date', '0', '0', '0' )"); } if ($crewchk == "0"){ mysql_query("UPDATE users SET rank='$newrank' WHERE username='$username'"); mysql_query("INSERT INTO `inbox` ( `id` , `to` , `from` , `message` , `date` , `read` , `saved` , `event_id` ) VALUES ( '', '$username', '$username', 'You have been promoted to $newrank your doing well!', '$date', '0', '0', '0' )"); } rankcheck(); Quote Link to comment https://forums.phpfreaks.com/topic/181910-query-question/#findComment-959478 Share on other sites More sharing options...
mikesta707 Posted November 17, 2009 Share Posted November 17, 2009 you don't end the function. stick a closing curly brace after the last if statement Quote Link to comment https://forums.phpfreaks.com/topic/181910-query-question/#findComment-959480 Share on other sites More sharing options...
Leveecius Posted November 17, 2009 Author Share Posted November 17, 2009 I see a few potential problems: $crewchk = mysql_query("SELECT crew FROM users WHERE username='$username'"); $crewboss = mysql_query("SELECT owner FROM crews WHERE owner='$owner'"); You run those two queries and assign the results to those two variable. THEN you try and use those two variable in an INSERT query. Those variables are not values, they are resource IDs to the result fo the queries. You need to extract the values. How would I go about doing that? That has always worked for me in the past. :S mysql_query("INSERT INTO `inbox` ( `id` , `to` , `from` , `message` , `date` , `read` , `saved` , `event_id` ) VALUES ( '', '$crewboss', '$username', 'You have a member ready to be promoted to $newrank! Let him rank?', '$date', '0', '0', '0' )"); Why include `id` in the INSERT if you are not going to provide a value? The id value is automatically incremented in my db so I don't need to add a value. }elseif ($crewchk == "0"); mysql_query("UPDATE users SET rank='$newrank' WHERE username='$username'"); Not even sure what that is supposed to do. You have a semicolon after the elseif clause so the elseif ends there and all code after it will be run without regard to the else if. I have since then removed it. Quote Link to comment https://forums.phpfreaks.com/topic/181910-query-question/#findComment-959486 Share on other sites More sharing options...
Leveecius Posted November 17, 2009 Author Share Posted November 17, 2009 you don't end the function. stick a closing curly brace after the last if statement That worked! Thanks man! I'll continue testing it to see if it works fully. Quote Link to comment https://forums.phpfreaks.com/topic/181910-query-question/#findComment-959492 Share on other sites More sharing options...
Psycho Posted November 17, 2009 Share Posted November 17, 2009 I see a few potential problems: $crewchk = mysql_query("SELECT crew FROM users WHERE username='$username'"); $crewboss = mysql_query("SELECT owner FROM crews WHERE owner='$owner'"); You run those two queries and assign the results to those two variable. THEN you try and use those two variable in an INSERT query. Those variables are not values, they are resource IDs to the result fo the queries. You need to extract the values. How would I go about doing that? That has always worked for me in the past. :S Really? Try echoing $crewchk or $crewboss to the page. You will get something like "Resource ID #17". You have to extract the values from the result set using one of the mysql_fetch function or similar functions. mysql_query("INSERT INTO `inbox` ( `id` , `to` , `from` , `message` , `date` , `read` , `saved` , `event_id` ) VALUES ( '', '$crewboss', '$username', 'You have a member ready to be promoted to $newrank! Let him rank?', '$date', '0', '0', '0' )"); Why include `id` in the INSERT if you are not going to provide a value? The id value is automatically incremented in my db so I don't need to add a value. So, don't include it in the list of fields to be populated as demonstrated in my examples. You don't have to list fields that have default values which you want populated with the default - kind of the point of a default value. Quote Link to comment https://forums.phpfreaks.com/topic/181910-query-question/#findComment-959500 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.