Jump to content

Why is my IF statement not working!?


sam06

Recommended Posts

Absolutely no idea why my IF statement's playing up - needs some fresh eyes I think!

 

if ($postcheck != $post){

if ($x == "0"){
	echo $title;
	echo "<br>";
	$x = "1";
	$y = $y + 1;
}
echo "Change Post to: ";
echo $postcheck;
echo " from ";
echo $post;
echo "<br>";

$result2 = mysql_query("UPDATE items SET Post='$postcheck' WHERE ID='$id'") 
or die(mysql_error()); 

}

 

When it returns $postcheck and $post, in this case, they're coming out as the same thing (on one record - no others!) - $post is retrieved from a database, and $postcheck is calculated. Both are numbers. Any ideas?!

Link to comment
https://forums.phpfreaks.com/topic/226497-why-is-my-if-statement-not-working/
Share on other sites

Running:

echo $postcheck;
echo "<br>";
echo $post;
echo "<br>";

before the if statement gives me:

1.89
1.89
Example Item 1
Change Post to: 1.89 from 1.89

 

The 1.89 and 1.89 look the same to me, so no idea why it's running the != statement...?!

 

Should expect no output

I would also suggest changing those echo statements to something  like this

echo "Postcheck: [$postcheck]<br>";
echo "Post: [$post]<br>";

 

It could be something as simple as a leading/trailing space, which the brackets should make apparent. You should also view the HTML source code to see if there is anything different.

Looks like a trailing space will cause them to compare differently, but not a leading space:

 

<?
$a = '1.89';
$b = ' 1.89';
if ($a != $b) print "[$a] != [$b]\n";
$b = '1.89 ';
if ($a != $b) print "[$a] != [$b]\n";
?>

 

Print out urlencode($post) is also a good way of finding any hidden characters.

Just changed the datatype in the DB to decimal.

Now is throwing up the same error in another record! :(

 

Using the brackets echo from above I now get:

 

Postcheck: [1.99]
Post: [1.99]

Postcheck: [1.19]
Post: [1.19]
Example Item 2
Change Post to: 1.19 from 1.19

Postcheck: [1.49]
Post: [1.49]

Well that is odd indeed.  Can you try printing the values like this:

 

echo "Postcheck: [" . urlencode($postcheck) . "]<br>";
echo "Post: [" . urlencode($post) . "]<br>";

 

If that shows them to be identical, try this:

 

echo "<pre>";
echo "Postcheck: "; var_dump($postcheck);
echo "Post: "; var_dump($post);

 

One of those should show up why they look the same but are unequal according to php.

I've changed it to reverse - if theyre the same, do nothing, else this - and that works!

 

It returns:

 

float 1.99

 

Post:

 

string '1.99' (length=4)

 

 

Postcheck:

 

float 1.19

 

Post:

 

string '1.19' (length=4)

 

 

Postcheck:

 

float 1.49

 

Post:

 

string '1.49' (length=4)

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.