The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Using PHP/MySQL, got a form that submits things to MySQL, how can I check to make sure that every entry is unique in that column? like, give an error when the user submits something that's already there in the MySQl db.

A couple of serious concerns with atomicmunky's code
1) printf() being used incorrectly
printf("We are sorry to inform you that the User Name <B><?php echo $Name ?></B> Is already Taken.");

This should never be done you can't embed <? ?> tags inside your print statement and it will surely cause a parse error furthermore printf() takes an argument and this should have been written like
printf("We are sorry to inform you that the User Name <B>%s</B> Is already Taken.", $Name);

2) in the else statement he inserts the data and then runs a query to get it out again what a waste of resources since if he inserted the data then the data is already available to the script and doesn't need to be retireved again.

3) the last printf() statement should be a regular print "" because no vars are being used, this can really cut down on processing time.

Please don't PM me with questions.
Use the forums, that is what they are here for.

I only needed the bit about if (mysql_num_rows($sqlquery) > 0) anyway...but yeah, I could see how those things could cause problems.

What is with those print, printf, and echos anyway? I normally uses echos, and seems easy enough to use as well, somehow it's easier to type the word echo, then print... for the above, I would have gone:

You are correct there isn't much difference the only thing I like printf() for is you can print a whole string with html and not worry about escaping double quotes or concatenating you vars into the statement take this for example

echo "<tr><td width=\"100\">". $somevar ."</td></tr>";

could be written

printf('<tr><td width="100">%s</td></tr>', $somevar);

The key is the %s that means substitue the arg given at the end with the %s in this case $somevar some other useful reasons to use it is to format numbers let's say you have a number like 34 and you want to display it as 000034 you could use printf('%06d', $num);

<Edited by freddydoesphp on 12-20-2000 at 11:09 PM>

Please don't PM me with questions.
Use the forums, that is what they are here for.

ok you turkeys, i never claimed to be a php expert.
i'm over here trying to help and for some reason you can't help but practically tell me that my code is evil and should be exorsized.
i appreciate the creative critisism though. i guess it gives me a higher "standard" to live up to

i'll give your "suggestion" a try freddy, but if it craps out on me, i'll know who to blame

Here's the problem:
When I submit an anchor name there's already in the db, it'll echo the msg about being more creative, but also the one about being Record updated/edited! And a new record is added, even though it should stop.

And I get a phrase error if I take the single quotes off the $anchor_name at the SELECT line, but all my other scripts never have quotes like that.
<Edited by Robo on 12-21-2000 at 01:41 AM>

The original error message that you obtained when setting the column unique was, I suspect, due to the fact that you already have multiple rows with an empty string value ("") in that column. As a result, MySQL complained because you were trying to set a column unique that was not, in fact unique to begin with.

You should always put quotes around any string value. In your example, if the string contained a space, a comma, or any number of special characters, your SQL statement would become invalid. The quotes ensure that MySQL can tell where the string begins and ends.