Forum Moderators: coopster

Message Too Old, No Replies

Checking For a Duplicate Entry

         

bodycount

4:35 pm on Nov 4, 2005 (gmt 0)

10+ Year Member



I have tried the following on a small PHP with only 2 entries and it works find, now I have extended the code a lot it does not seem to work. It is coming up with an error and still putting the entry into the database. It is coming up with the following error "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/body1975/public_html/input.php on line 143"

line 143 is " $num_rows=mysql_num_rows($mysql_result);" I have put stars next to the offending line of code. Can someone tell me what I have done wrong.

-------------------------------------------------------

$FULLNAME = empty($_POST['FULLNAME'])? die ("ERROR: Enter a Fullname") : mysql_escape_string($_POST['FULLNAME']);
$YOB = empty($_POST['YOB'])? die ("ERROR: Enter a Year of Brith if UNKNOWN put n/a") : mysql_escape_string($_POST['YOB']);

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// duplicate entry
$sql="SELECT * FROM `ADDRESS` WHERE FULLNAME='$FULLNAME'AND YOB='$YOB'";
$mysql_result=mysql_query($sql);
***** $num_rows=mysql_num_rows($mysql_result); ****
if ( $num_rows == 0) {
echo "No Duplicates Found<br>";

[edited by: jatar_k at 4:52 pm (utc) on Nov. 4, 2005]
[edit reason] reduced code dump [/edit]

jatar_k

4:46 pm on Nov 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



your query is dying

// duplicate entry
$sql="SELECT * FROM `ADDRESS` WHERE FULLNAME='$FULLNAME'AND YOB='$YOB'";
$mysql_result=mysql_query($sql);
***** $num_rows=mysql_num_rows($mysql_result); ****

the query you are building there is causing an error. To return the proper error change your query line to

$mysql_result=mysql_query($sql) or die(mysql_error());

bodycount

4:55 pm on Nov 4, 2005 (gmt 0)

10+ Year Member



Thanks for the help jatar_k

it was this line what was causing the error "$sql="SELECT * FROM ADDRESS WHERE FULLNAME='$FULLNAME'AND YOB='$YOB'";"

It would help if I could spell ADDRESSES right instead of putting "ADDRESS" But how did the entries make it to the ADDRESSES table?

gsnider

8:23 pm on Nov 4, 2005 (gmt 0)

10+ Year Member



when you used your insert function you didn't mistype it?

bodycount

11:54 pm on Nov 4, 2005 (gmt 0)

10+ Year Member



oh yeah, doh.

directrix

1:19 am on Nov 5, 2005 (gmt 0)

10+ Year Member



If you just want to determine whether there are duplicates, it is better to code:

SELECT COUNT(*) FROM 'ADDRESS' WHERE FULLNAME='$FULLNAME' AND YOB='$YOB'

This does not force the SQL engine to return the rows themselves; just the count. (Even that is overkill. Ideally we would stop the query once more than one row is counted. I've seen elegant implementations of this idea on other platforms, but I don't know enough about MySQL to comment further.)

NomikOS

1:30 am on Nov 5, 2005 (gmt 0)

10+ Year Member



$sql="SELECT * FROM `ADDRESS` WHERE FULLNAME='$FULLNAME'AND YOB='$YOB'";

`ADDRESS` between quotes?

jakar_t is right in that way you obtain more info about mistakes. You did it?