# What is wrong with this query?

**URL:** <https://forum.kirupa.com/t/what-is-wrong-with-this-query/204853>\
**Category:** Uncategorized\
**Created:** [October 24, 2006, 2:13am UTC](https://forum.kirupa.com/t/what-is-wrong-with-this-query/204853 "2006-10-24T02:13:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flash\_Man1](https://avatars.discourse-cdn.com/v4/letter/f/8797f3/32.png) [@Flash\_Man1](https://forum.kirupa.com/u/Flash_Man1)\
**Post date:** [October 24, 2006, 2:13am UTC](https://forum.kirupa.com/t/what-is-wrong-with-this-query/204853/1 "2006-10-24T02:13:48Z")

</div>

here is the error:  
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF (SELECT \* FROM users WHERE firstname \<\> ‘’ AND email \<\> ‘’)"

and here is my code:

CODE

```php
<?php

mysql_pconnect("$host","$user","$pass") or die (mysql_error());
mysql_select_db("$db") or die (mysql_error());

$name = $_POST['fname'];
$email = $_POST['email'];

$query = "INSERT INTO users (`firstname`, `email`) VALUES ('$fname', '$email') IF (SELECT * FROM users WHERE firstname <> '$name' AND email <> '$email', )";
//$query = "SELECT * FROM users WHERE firstname <> '$name' AND email <> '$email'"
//$query1 = "INSERT INTO users (`firstname`, `email`) VALUES ('$fname', '$email') IF email <> '$email'"

$result = mysql_query( $query ) or die (mysql_error());
//$result1 = mysql_query( $query1 ) or die (mysql_error());

$num = mysql_num_rows( $result );
if ($num == 1){
    print "status=Same user&checklog=1";
    } else {
    print "status=New user&checklog=2";
}

?>

```

---

<div class="post-metadata">

**Author:** ![evildrummer](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/evildrummer/32/2308_2.png) [@evildrummer](https://forum.kirupa.com/u/evildrummer)\
**Post date:** [October 24, 2006, 12:21pm UTC](https://forum.kirupa.com/t/what-is-wrong-with-this-query/204853/2 "2006-10-24T12:21:10Z")

</div>

Why are you using an IF statement in your SQL?

---

<div class="post-metadata">

**Author:** ![bwh2](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bwh2](https://forum.kirupa.com/u/bwh2)\
**Post date:** [October 24, 2006, 1:41pm UTC](https://forum.kirupa.com/t/what-is-wrong-with-this-query/204853/3 "2006-10-24T13:41:00Z")

</div>

are you just trying to check whether that combination of first name and email exist in the table? if they don’t exist, insert?

---

<div class="post-metadata">

**Author:** ![Flash\_Man1](https://avatars.discourse-cdn.com/v4/letter/f/8797f3/32.png) [@Flash\_Man1](https://forum.kirupa.com/u/Flash_Man1)\
**Post date:** [October 25, 2006, 12:10am UTC](https://forum.kirupa.com/t/what-is-wrong-with-this-query/204853/4 "2006-10-25T00:10:52Z")

</div>

yes exactly thats exactly what i’m trying to do…

well basically yeh but just do the checking only with the email so if the email is the same don’t insert.

cheers

---

<div class="post-metadata">

**Author:** ![Flash\_Man1](https://avatars.discourse-cdn.com/v4/letter/f/8797f3/32.png) [@Flash\_Man1](https://forum.kirupa.com/u/Flash_Man1)\
**Post date:** [October 25, 2006, 12:11am UTC](https://forum.kirupa.com/t/what-is-wrong-with-this-query/204853/5 "2006-10-25T00:11:44Z")

</div>

Thanks bwh2 😃 is my sql wrong with this or how would i write the query correctly??

---

<div class="post-metadata">

**Author:** ![Flash\_Man1](https://avatars.discourse-cdn.com/v4/letter/f/8797f3/32.png) [@Flash\_Man1](https://forum.kirupa.com/u/Flash_Man1)\
**Post date:** [October 25, 2006, 12:36am UTC](https://forum.kirupa.com/t/what-is-wrong-with-this-query/204853/6 "2006-10-25T00:36:15Z")

</div>

hey i’ve tried having a play in php here is what i’ve come up with:

```php

$query = mysql_query("INSERT INTO users (`firstname`, `email`) VALUES ('$fname', '$email')");
$query2 = mysql_fetch_array($query);
if ($query2 != '') {
print "status=Same user&checklog=1";
} else {
mysql_query ("SELECT * FROM users WHERE firstname = '$name' AND email = '$email'");
print "status=New user&checklog=2";
}

```

yet it still adds and does not check or execute the query…just keeps adding same email and same email lol

---

<div class="post-metadata">

**Author:** ![bwh2](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bwh2](https://forum.kirupa.com/u/bwh2)\
**Post date:** [October 25, 2006, 3:55am UTC](https://forum.kirupa.com/t/what-is-wrong-with-this-query/204853/7 "2006-10-25T03:55:43Z")

</div>

if i understand you right, you probably want to do something like this:

```php

/* test if user exists */
$sql = "SELECT * FROM users WHERE email = '$email'";
$result = mysql_query( $sql );

/* if user does not exist */
if( mysql_num_rows($result) == 0 ) {

	/* insert user into db */
	$sql = "INSERT INTO users (firstname, email) VALUES ('$fname', '$email')";
	$result = mysql_query( $sql ) or die('Could not add user');
}

```

---

<div class="post-metadata">

**Author:** ![Flash\_Man1](https://avatars.discourse-cdn.com/v4/letter/f/8797f3/32.png) [@Flash\_Man1](https://forum.kirupa.com/u/Flash_Man1)\
**Post date:** [October 25, 2006, 7:03am UTC](https://forum.kirupa.com/t/what-is-wrong-with-this-query/204853/8 "2006-10-25T07:03:18Z")

</div>

cool thanks heaps bwh2…it only inserts the email into the database tho and not the name which is really odd?? because the sql code for the insert into line is all correct i’m sure of it and the fields and what not are all correct to… can you think of anything??

---

<div class="post-metadata">

**Author:** ![Flash\_Man1](https://avatars.discourse-cdn.com/v4/letter/f/8797f3/32.png) [@Flash\_Man1](https://forum.kirupa.com/u/Flash_Man1)\
**Post date:** [October 25, 2006, 7:13am UTC](https://forum.kirupa.com/t/what-is-wrong-with-this-query/204853/9 "2006-10-25T07:13:57Z")

</div>

all fixed i just didn’t see and extra letter where we called the variable in the insert query $fname should have been $name 🙂 cheers again for the help
