ERROR that cant be found.HELP

i have these code below,keep goes to “else” condition no matter what number i put whether it is the same or not, I really don’t know why, Please help me to figure it out. Thanks!


<script type="text/javascript">
    function update()
    {
    var msg = "DATA SUCCESSFULLY UPDATED!";
    alert(msg);
    window.history.back();
    }
</script>

<script type="text/javascript">
    function no_update()
    {
    var msg = "NO DATA UPDATED!";
    alert(msg);
    window.history.back();
    }
</script>

<?php


$z = $_POST['original'] ;
$x = $_POST['brand'];
$w = $_POST['copy'] ;

echo $_POST['original'];
echo $_POST['copy'];
echo $_POST['brand'];

/*
switch ($z)

{
	case $x:
	echo '<script type="text/javascript"> no_update(); </script>';

		
	default:
	echo '<script type="text/javascript"> update(); </script>';
}
*/

if ($z == $w)
	{	
		echo '<script type="text/javascript"> no_update(); </script>';
		
	}
else
	{	
		
		echo $_POST['original'];
		echo $_POST['copy'];
		echo $_POST['brand'];
		$con = mysql_connect("localhost","root","880702");
		if (!$con)
		  {
		  die('Could not connect: ' . mysql_error());
		  }

		mysql_select_db("SPAREPART2", $con);


		mysql_query("UPDATE list2 SET Qty = '".$z."' WHERE Brand = '".$x."'"); /* no error */

		echo '<script type="text/javascript"> update(); </script>';

		
	}

mysql_close($con);
?> 



p/s: The value of $z and $w is an integer number (ex = 32,33.55,44).

How are you testing (calling) this code? What does the submit form look like?
What have you done to confirm that the inputs ARE integers (BTW: 33.55 is not an integer. Perhaps the dot was a mis-typed comma)

Before the

if ($z == $w) {

add

echo "Z= {$Z}<br>W= {$W}<br>";

That should tell you.

$z and $w are strings.

Before the if statement var_dump($z) and var_dump($w) to see the exact values.

ive tested it by echo-ing it before tried to use it if you notice in the code i posted. actually its 33,55. mistyped. the submit form looks like this

<form name=“lol” action=“the_code_i_posted_just_now.php” method=“post”>
<label> Brand</label>
       :
<input id=“brand” name=“brand” type=“text” class = “lol” readonly=“readonly” value= “<?php echo $c; ?>”/>
    </p>

       &lt;input  name="original" type="text" class = "quantity" value= "&lt;?php echo $l; ?&gt; " /&gt;
      &lt;input  name="copy" type="text" class = "quantity" value= "&lt;?php echo $l; ?&gt;" style="visibility: hidden" /&gt;

</form>

p/s: $c and $l is an integer value.

i’ve already did that in “else” condition if you notice it in the code.

i’ve already “echo” it before use and it echo the correct value. is it different by using var_dump?

FOUND IT. Thanks to John Betong. After using var_dump(), i got string(3) “23” for $z and string(2) “23” for $w. Why the string is not the same but the value is the same? can somebody explain?

You might have accidentally got a space when typing in the value, trim() can be used to eliminate blank spaces at the start and end of strings

how can i use the trim()? just simply put trim($z); before using it?

FIXED! THANK YOU SO MUCH FOR YOUR HELP ESPECIALLY JOHN BETUNG AND SPACEPHOENIX.^^

'glad you managed to find why your script was not doing what you wanted it to do :slight_smile:

PHP’s automatic type conversion still trips me up and frequently Google “php manual type_juggling”

http://php.net/manual/en/language.types.type-juggling.php

Please note that $_POST is an array of strings and useful tests are:



# check for $_POST and if set then show all string values 
if( count( $_POST) > 0 )
{
  # PRE required for formatting with line-feeds
  echo '<pre>';  
    print_r($_POST);
  echo '</pre>';

  # convert to integers OR set a default value
  $z = isset( $_POST['original'] ) ? intval( $_POST['original'] ) : 0;
  $x = isset( $_POST['brand']    ) ? intval( $_POST['brand']   ) : 0;;
  $w = isset( $_POST['copy']     ) ? intval( $_POST['copy']    ) : 0;    
}
else
{
   echo 'Yes we have no $_POST today';
}