If statement is true , but its not working in PHP

Please see below code, when if statement is true then its showing the result of else statement.

<?php
	include 'db_connection.php';
	include 'reader.php';	
		$file="sample.xls";
		$connection=new Spreadsheet_Excel_Reader();
		$connection->read($file);	
		$startrow=1;
		$endrow=2;
		$col1=1;
		$col2=2;
		$col3=3;
		for($i=$startrow;$i<$endrow;$i++){
		$rrt1= $connection->sheets[0]["cells"][$i][$col1]."<br>";		
		$rrt2= $connection->sheets[0]["cells"][$i][$col2]."<br>";		
		$rrt3= $connection->sheets[0]["cells"][$i][$col3]."<br>";		
		echo $rrt1;		
		echo $rrt2;		
		echo $rrt3;			
		}
		if ($rrt1 == "Name" || $rrt2 == "job" || $rrt3 == "email") {
		$x=2;
		while($x<=$connection->sheets[0]['numRows']) {
		$name = isset($connection->sheets[0]['cells'][$x][1]) ? $connection->sheets[0]['cells'][$x][1] : '';
		$job = isset($connection->sheets[0]['cells'][$x][2]) ? $connection->sheets[0]['cells'][$x][2] : '';
		$email = isset($connection->sheets[0]['cells'][$x][3]) ? $connection->sheets[0]['cells'][$x][3] : '';
			
		$sql_insert="INSERT INTO users_details (id,name,job,email) VALUES ('','$name','$job','$email')";
		$result_insert = mysqli_query($conn,$sql_insert) or die(mysql_error()); 
			 
		  $x++;
		}
		}
		else {
echo "Not matching!";

}
?>

Getting below result.

Hi mehiddy welcome to the forum

If statement is true

But is it? Might it be something like " Name " not being equal to “Name” ?

Try trim() ing the variables.

Hello dear, Its still same, please see below.

<?php
	include 'db_connection.php';
	include 'reader.php';	
		$file="sample.xls";
		$connection=new Spreadsheet_Excel_Reader();
		$connection->read($file);	
		$startrow=1;
		$endrow=2;
		$col1=1;
		$col2=2;
		$col3=3;
		for($i=$startrow;$i<$endrow;$i++){
		echo $rrt1= $connection->sheets[0]["cells"][$i][$col1]."<br>";		
		echo $rrt2= $connection->sheets[0]["cells"][$i][$col2]."<br>";		
		echo $rrt3= $connection->sheets[0]["cells"][$i][$col3]."<br>";		
		$txt1 = trim(preg_replace('/\s{2}+/','',$rrt1));
		echo $txt1;
		}
	  if ($txt1 == "Name") 
					{
		$x=2;
		while($x<=$connection->sheets[0]['numRows']) {
		$name = isset($connection->sheets[0]['cells'][$x][1]) ? $connection->sheets[0]['cells'][$x][1] : '';
		$job = isset($connection->sheets[0]['cells'][$x][2]) ? $connection->sheets[0]['cells'][$x][2] : '';
		$email = isset($connection->sheets[0]['cells'][$x][3]) ? $connection->sheets[0]['cells'][$x][3] : '';
			
		$sql_insert="INSERT INTO users_details (id,name,job,email) VALUES ('','$name','$job','$email')";
		$result_insert = mysqli_query($conn,$sql_insert) or die(mysql_error()); 
			 
		  $x++;
		}
		 } else {
echo "Not matching!";
		 }

?>

here is the output

got the solution as below.

<?php
		include 'db_connection.php';
		include 'reader.php';	
			$file="sample.xls";
			$connection=new Spreadsheet_Excel_Reader();
			$connection->read($file);	
			$startrow=1;
			$endrow=2;
			//$col1=1;
			//$col2=2;
			//$col3=3;
			for($i=$startrow;$i<$endrow;$i++){
			$rrt1= $connection->sheets[0]["cells"][1][1]."";		
			$rrt2= $connection->sheets[0]["cells"][1][2]."";		
			$rrt3= $connection->sheets[0]["cells"][1][3]."";		
			//var_dump($rrt1);		
			//var_dump($rrt2);		
			//var_dump($rrt3);			
			}
			if ($rrt1 == "Name" && $rrt2 == "job" && $rrt3 == "email2") {
			$x=2;
			while($x<=$connection->sheets[0]['numRows']) {
			$name = isset($connection->sheets[0]['cells'][$x][1]) ? $connection->sheets[0]['cells'][$x][1] : '';
			$job = isset($connection->sheets[0]['cells'][$x][2]) ? $connection->sheets[0]['cells'][$x][2] : '';
			$email = isset($connection->sheets[0]['cells'][$x][3]) ? $connection->sheets[0]['cells'][$x][3] : '';
				
			$sql_insert="INSERT INTO users_details (id,name,job,email) VALUES ('','$name','$job','$email')";
			$result_insert = mysqli_query($conn,$sql_insert) or die(mysql_error()); 
				 
			  $x++;
			}
			}
			else {
    echo "Not matching!";
}
        ?>
1 Like

I’m not sure how you would think that this:

$rrt1= $connection->sheets[0]["cells"][$i][$col1]."<br>";

would ever result in a string called $rrt1 containing "Name". Surely it would be a string containing "Name<br>"?

As you’ve seen, once you remove the "<br>" from the end, it works correctly. It doesn’t show in the echo() statement because the html is rendered rather than displayed.

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.