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.
check if entry exists before inserting duplicant entry?
Moderator: General Moderators
-
coreycollins
- Forum Commoner
- Posts: 67
- Joined: Sun Feb 01, 2004 1:04 pm
- Location: Michigan
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
?>Also described here viewtopic.php?t=19442