Jump to content

Select in an IF-Statement


PAGO

Recommended Posts

Hey so I am back with another question:

 

I want to make an INSERT in my DB, but only if 2 entries doesn't already exist.

                $select1 = mysqli_query($con,"SELECT COUNT(email) FROM kunde WHERE email = '$email'");
  		
  		$select2 = mysqli_query($con,"SELECT COUNT(adresse) FROM kunde WHERE adresse = '$adresse'");
 
  		
  		if ($select1 = 0 && $select2 = 0)
  		{
  			$sqlkunde ="INSERT INTO kunde (kunde_geschlecht, vorname, nachname, email, adresse) 
			VALUES
			('$geschlecht',
			'$vorname',
			'$nachname',
			'$email',
			'$adresse')";
			mysqli_query($con,$sqlkunde);
  		}

So I let my code count how often my variable already exists in in the table. If the variable ($email, $adresse) doesn't exist, the outcome of my count should be 0 and he executes the INSERT. If not he does nothing.

 

The INSERT works, so the problem has to do with the value which stands (.. or doesn't stand) behind my variables $select1/2.

 

Thanks in advance,

 

PAGO

Link to comment
https://forums.phpfreaks.com/topic/286923-select-in-an-if-statement/
Share on other sites

mysqli_query only returns a result set, you need to use a mysqli_fetch_* function to retrieve data from that result set, eg

$result1 = mysqli_query($con,"SELECT COUNT(email) FROM kunde WHERE email = '$email'");
$select1 = mysqli_fetch_row($result1);

$result2 = mysqli_query($con,"SELECT COUNT(adresse) FROM kunde WHERE adresse = '$adresse'");
$select2 = mysqli_fetch_row($result2);

if($select1[0] != 0 && $select2[0] != 0)
{
    // do insert
}

Alternatively you could make the email and adresse fields unique keys, then use a an INSERT IGNORE query instead

I don't know why, but suddently it works. I forgot [] always means an array ;).

 

I changed the = to != and placed the INSERT in the ELSE section.

Before I had $select1[0] = 0 and had the INSERT in the TRUE section of the IF.

For me it didn't changed the context at all but it seems php does likes this more ;)

 

Here is the working code, for anyone with the same problem:

                $result1 = mysqli_query($con,"SELECT COUNT(*) FROM kunde WHERE email = '$email'");
		$select1 = mysqli_fetch_row($result1);

		$result2 = mysqli_query($con,"SELECT COUNT(*) FROM kunde WHERE adresse = '$adresse'");
		$select2 = mysqli_fetch_row($result2);

		if($select1[0] != 0 && $select2[0] != 0)
  		{
  		} else {
  			$sqlkunde ="INSERT INTO kunde (kunde_geschlecht, vorname, nachname, email, adresse) 
			VALUES
			('$geschlecht',
			'$vorname',
			'$nachname',
			'$email',
			'$adresse')";
			mysqli_query($con,$sqlkunde);
  		}

Thanks again. This Forum is awsome ;)

Why are you writing "$select1[0]", what does the 0 stand for?

I always thought, that i have to entere the column here.

 

 

Because I am using mysql_fetch_row, not mysql_fetch_assoc

 

I changed the = to != and placed the INSERT in the ELSE section.

My bad, != should of been ==

if($select1[0] == 0 && $select2[0] == 0)
{
    // do insert
}

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.