What is wrong with this query?

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

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";
}



?>

Why are you using an IF statement in your SQL?

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

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

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

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


$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

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


/* 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');
}

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

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