Page 1 of 1

check if entry exists before inserting duplicant entry?

Posted: Wed Mar 17, 2004 3:31 pm
by jameso
Before adding a user to my MySQL database I want to check to make sure they don't exist already. I want to validate their ID, FirstName, and LastName before running the query to insert them into the database.
I can provide any code I'm working with if you want but this seems like a simple IF or maybe even a built-in feature of MySQL(?).

Any suggestions? It'd be nice to turn this into a function too, for later use.

Thanks in advance.

Posted: Wed Mar 17, 2004 5:01 pm
by coreycollins
The way I ususally do it is by checking the data against the database. I just do a SELECT with the user supplied information. I don't know if this is the best way though...

Posted: Wed Mar 17, 2004 5:32 pm
by tim
this works for me and i like it:

Code: Select all

<?php
$sel = "SELECT username FROM table_name where username = '$username'";
$quer = mysql_query($sel);
$row = mysql_fetch_array($quer);
$final = $row["user"];

if ($final == "") {
$result = "INSERT INTO etc etc etc"; 
mysql_query($result);
echo "Thanks for signing up $username";
} else {
echo "the name, $username, has been taken";}

// something to that effect
?>

Posted: Wed Mar 17, 2004 5:52 pm
by JAM
Also described here viewtopic.php?t=19442

Posted: Sat Apr 03, 2004 4:20 am
by jameso
Thanks!