Jump to content

Sql error


deansaddigh

Recommended Posts

Ok i was just missing the '' around email .

For somereason its still inserting to db even if email exists, can anyone show me what im doing wrong.

Here is the code for the check if email exists .

//check again db if user email allready exists
$emailcheck =" SELECT * FROM student
WHERE email ='$email'";
$result = mysql_query($emailcheck)
	or die("error on email check");
$row = mysql_fetch_row($result);

if(!isset($row['email']))

{
		//Insert enquiry data 
	 $query = "INSERT INTO student(student_id, email, first_name, second_name, street, town, country, postcode, password)
	VALUES(0, '$email', '$firstname', '$surname', '$street', '$town', '$country', '$postcode', '$password')";
	$result = mysql_query($query) 
		or die("Error making contact");
} 
else 
{
   	  echo 'this email address already exists';
}  

 

Can anyone see what im doing wrong

Link to comment
https://forums.phpfreaks.com/topic/203619-sql-error/#findComment-1066564
Share on other sites

ive looked and changed it to mysql num rows, is it possible to use issett with it like this

 

$row = mysql_num_rows($result);

if(!isset($row['email']))

{
		//Insert enquiry data 
	 $query = "INSERT INTO student(student_id, email, first_name, second_name, street, town, country, postcode, password)
	VALUES(0, '$email', '$firstname', '$surname', '$street', '$town', '$country', '$postcode', '$password')";
	$result = mysql_query($query) 
		or die("Error making contact");
} 

 

I cant find out if it is, i have tried executing the code and it doesnt seem to work

Link to comment
https://forums.phpfreaks.com/topic/203619-sql-error/#findComment-1066573
Share on other sites

Thanks for the help got it working with your help.

 

//check again db if user email allready exists
 $emailcheck =" SELECT * FROM student
WHERE email ='$email'";
$result = mysql_query($emailcheck)
	or die("error on email check");
$row = mysql_num_rows($result);

if( mysql_num_rows($result) == 0 )

{
	//Insert enquiry data 
	 $query = "INSERT INTO student(student_id, email, first_name, second_name, street, town, country, postcode, password)
	VALUES(0, '$email', '$firstname', '$surname', '$street', '$town', '$country', '$postcode', '$password')";
	$result = mysql_query($query) 
		or die("Error making contact");
} 
else 
{
   	  	echo 'this email address already exists';
}  

 

Only question is why is this

if( mysql_num_rows($result) == 0 )

and not

if( mysql_num_rows($row) == 0 )

?

I dont understand do i not need this line then

$row = mysql_num_rows($result);

Link to comment
https://forums.phpfreaks.com/topic/203619-sql-error/#findComment-1066585
Share on other sites

Does it matter which way you do it, how would you do it?

Thanks by the way

<?php
$sql = " SELECT * FROM student WHERE email ='$email'";
if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
        echo 'this email address already exists';
    } else {
        $sql = "
            INSERT INTO student(
                student_id, email, first_name, second_name, street, town, country, postcode, password
            ) VALUES (
                0, '$email', '$firstname', '$surname', '$street', '$town', '$country', '$postcode', '$password'
        )";
        if (!mysql_query($sql)) {
            trigger_error(mysql_error());
        }
    }
} else {
    trigger_error(mysql_error());
}

Link to comment
https://forums.phpfreaks.com/topic/203619-sql-error/#findComment-1066913
Share on other sites

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.