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
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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

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.