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