DuaneCawaling Posted March 3, 2013 Share Posted March 3, 2013 $username = "root"; $password = "duane"; $hostname = "localhost"; $con = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); echo "Connected to MySQL<br>"; $selected = mysql_select_db("dummy") or die ("No existing database".mysql_error()); $number = "$_POST[number]"; $keyword = $message[2]; if($keyword == "balance"||"BALANCE"){ $reply = mysql_query("SELECT * from profile where contact = '$number'"); $info = mysql_fetch_array($reply); echo "Your remaining balance is: ".$info['credit']."<br />"; }else if ($keyword == "menu"||"MENU") { $data = mysql_query("SELECT * from food") or die (mysql_error()); while ($row = mysql_fetch_array($data)) { echo "<strong>Item(</strong>".$row{'menu_tag'}."<strong>):</strong> ".$row{'item'} ." --<em>price</em>:".$row{'price'}."<br>";//display the results } }else if ($keyword == "snacks"||"SNACKS") { $data = mysql_query("SELECT * from inventory") or die (mysql_error()); while ($row = mysql_fetch_array($data)) { echo "<strong>Item(</strong>".$row{'tag'}."<strong>):</strong> ".$row{'item'} ." --<em>price</em>:".$row{'price'}."<br>";//display the results } } there are no syntax errors. my problem is that it keeps on entering the FIRST if statement even if the $keyword is not "balance" @@ Quote Link to comment https://forums.phpfreaks.com/topic/275146-if-statement-error/ Share on other sites More sharing options...
KevinM1 Posted March 3, 2013 Share Posted March 3, 2013 You can't do ifs like that. It needs to be: if ($keyword == "balance" || $keyword == "BALANCE") That said, rather than check for case (what if the keyword comes back as "BaLAnCe"?), just run it through strtolower. Also, where does $message come from? Quote Link to comment https://forums.phpfreaks.com/topic/275146-if-statement-error/#findComment-1416127 Share on other sites More sharing options...
teynon Posted March 3, 2013 Share Posted March 3, 2013 To further explain KevinM1's answer. An if statement uses boolean logic. Here is an example of some boolean logic: true AND false = false true OR false = true (true or false) && true = true In your if statement, you are passing two individual booleans in that statement. if($keyword == "balance"||"BALANCE") // breaks down into: if (($keyword == "balance") || "BALANCE") if $keyword == "balance" works as you expect, but the second part is just checking a static string's value. PHP considers a non empty string to be TRUE in boolean logic, so what you've actually written is this: if ($keywords == "balance" || true) So as KevinM1 said, you need to change all your if statements to be if ($keyword == "monkeys" || $keyword == "MONKEYS") { You could also try something like this: if (strtolower($keyword) == "monkeys") This would make matching the string case insensitive. Quote Link to comment https://forums.phpfreaks.com/topic/275146-if-statement-error/#findComment-1416198 Share on other sites More sharing options...
DuaneCawaling Posted March 3, 2013 Author Share Posted March 3, 2013 thanks for the replies. i was thinking so hard what was my problem..it was just the wrong use of or sign..thank you again, really now!a function for making strings to lower case..that will make things more easy Quote Link to comment https://forums.phpfreaks.com/topic/275146-if-statement-error/#findComment-1416226 Share on other sites More sharing options...
haku Posted March 4, 2013 Share Posted March 4, 2013 (edited) if (strtolower($keyword) == "snacks") {etc. Edited March 4, 2013 by haku Quote Link to comment https://forums.phpfreaks.com/topic/275146-if-statement-error/#findComment-1416355 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.