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
Share on other sites

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?

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.