Jump to content

if statement error


DuaneCawaling

Recommended Posts



$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" @@

Link to comment
https://forums.phpfreaks.com/topic/275146-if-statement-error/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/275146-if-statement-error/#findComment-1416198
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.