Jump to content

Select in an IF-Statement


PAGO
Go to solution Solved by Ch0cu3r,

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

Edited by PAGO
Link to comment
Share on other sites

  • Solution

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

Edited by Ch0cu3r
Link to comment
Share on other sites

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 ;)

Edited by PAGO
Link to comment
Share on other sites

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