I am trying to compare two variables one is getting fetched from database and other is a textbox value.
If comparison is successful then the if statement block will return the value as matched and if they don't else statement block will return not matched
.
But it is always returning not matched
even if I enter same value that is in the database.
My code is this:
<?php
error_reporting(0);
$user_id = 5;
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('qurefle');
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
if(isset($_POST["blccrcc"])){
$sql = 'SELECT l_coupon_c FROM cc_generator WHERE unique_id = "'.$user_id.'"';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_assoc($retval))
{
//fetching data
$l_coupon_c = $row['l_coupon_c'];
//Data Comparison
$friend_cc = $_POST["lccrcc"];
if ($l_coupon_c == $friend_cc) {
echo"it is matched";
}
else{
echo"Not Matched";
}
}//while bracket
}//if isset bracket
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>LCC AND RCC Coupon Code Chain Extender</title>
</head>
<body>
<center>
<form method="POST" action="">
Friend cc: <input type="text" name="lccrcc"><br><br>
<input type="submit" value="Use Coupon Code" name="blccrcc">
</form>
</center>
</body>
</html>
Use trim
as well, sometimes there are some additional space issues too.
if(trim($l_coupon_c) == trim($friend_cc))
and print_r($_POST);
to see the values of POST
please pass character in capital letter(UPPERCASE) as per your database screenshot your data stored in upepercase or convert form data into uppercase using function strtoupper()
refer this link : http://php.net/manual/en/function.strtoupper.php
i.e : strtoupper($_POST['lccrcc']);
it works in your case if you pass data in lowercase. or do same in reverse order with convert your stored data into lowercase with help of strtolower()
refer this link : http://php.net/manual/en/function.strtolower.php