Jump to content

Simple problem!


unistake

Recommended Posts

Hi all,

 

I have a problem with an IF statement. I have two rows in my database with the same email address. Have a look at the code to see what I am trying to do!

 

The problem is that the IF statement is only recognising the 1st row in my database, when I want it to recognise all the rows with the same email address.

 

Hope you can help!

 

Thanks

 

<?php
$sql = "SELECT reg FROM sales WHERE email='$_SESSION[logname]'";
$result = mysqli_query($cxn,$sql)
	or die ("Couldn't execute query");

$row = mysqli_fetch_assoc($result);
if ($row['reg'] != $_GET['reg'] )
	{
		echo "I am sorry but  '$_GET[reg]' does not seem to be registered by you!";
		exit();
	}
if ($row['reg'] == $_GET['reg'] )
	{
		$data = $row['reg'];
	}
?>

Link to comment
https://forums.phpfreaks.com/topic/208918-simple-problem/
Share on other sites

Your problem is that $row only contains one row of data from the db.

 

What I would do is something like this:

<?php
$sql = "SELECT reg FROM sales WHERE email='$_SESSION[logname]'";
$result = mysqli_query($cxn,$sql)
	or die ("Couldn't execute query");
        $data = array(); //This way we can hold multiple results
        $i = 0; //The index of the array to add the result to

while($row = mysqli_fetch_assoc($result))
       {
            if ($row['reg'] != $_GET['reg'] )
    {
		echo "I am sorry but  '$_GET[reg]' does not seem to be registered by you!";
		exit();
    }
    else { //Why use two if statements?
		$data[$i] = $row['reg'];
                        $i++; //increase the array index for the next while loop
    }
       }
?>

 

Link to comment
https://forums.phpfreaks.com/topic/208918-simple-problem/#findComment-1091259
Share on other sites

Actually it doesnt seem to work, now the 2nd row is found but not the first ! :)

 

The joys of PHP!

 

<?php
$sql = "SELECT reg FROM sales WHERE email='$_SESSION[logname]'";
$result = mysqli_query($cxn,$sql)
	or die ("Couldn't execute query");
$data = array();
    $i = 0; 

$row = mysqli_fetch_assoc($result);
while($row = mysqli_fetch_assoc($result))
       {
if ($row['reg'] != $_GET['reg'] )
	{
		echo "I am sorry but this aircraft '$_GET[reg]' does not seem to be registered by you!";
		exit();
	}
else { 
	$data[$i] = $row['reg'];
	$i++;
              }
} 
}
?>

Link to comment
https://forums.phpfreaks.com/topic/208918-simple-problem/#findComment-1091263
Share on other sites

still is not working, both rows are saying they are not registered with the email address.

 

<?php 
$sql = "SELECT reg FROM sales WHERE email='$_SESSION[logname]'";
$result = mysqli_query($cxn,$sql)
	or die ("Couldn't execute query");
$data = array(); //This way we can hold multiple results
    $i = 0; //The index of the array to add the result to

while($row = mysqli_fetch_assoc($result))
       {
	if ($row['reg'] != $_GET['reg'] )
		{
			echo "I am sorry but this aircraft '$_GET[reg]' does not seem to be registered by you!";
			exit();
		}
	else { //Why use two if statements?
		$data[$i] = $row['reg'];
		$i++; //increase the array index for the next while loop	
		}
	} 

?>

Link to comment
https://forums.phpfreaks.com/topic/208918-simple-problem/#findComment-1091290
Share on other sites

I am not sure what you mean by both the row values together.

 

while($row = mysqli_fetch_assoc($result))
       {
	echo $row['reg'] . "<br>";
} 

The above code should output the values in the database, one on each line. These values should equal $_GET['reg'] for the script to work.

Link to comment
https://forums.phpfreaks.com/topic/208918-simple-problem/#findComment-1091300
Share on other sites

Maybe try swithing the if statement to if($row['reg'] == $_GET['reg']) and swap the error message etc respectively. If the two variables match each other, then the if statement should pick it up fine. If it doesnt, then I am out of ideas (in which case starting a new thread may help as people will probably think you got the help you needed on this one).

Link to comment
https://forums.phpfreaks.com/topic/208918-simple-problem/#findComment-1091316
Share on other sites

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.