Insert names into table if not already existing

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2012-09-12T03:33:51Z Indexed on 2012/09/12 3:38 UTC
Read the original article Hit count: 171

Filed under:
|

I am trying to build an application that allows users to submit names to be added to a db table (runningList). Before the name is added tho, I would like to check firstname and lastname against another table (controlList). It must exist in (controlList) or reply name not valid. If the name is valid I would like to then check firstname and lastname against (runningList) it make sure it isnt already there. If it isnt there, then insert firstname & lastname. If it is there, reply name already used.

Below is code that worked before I tried to add the test against the controlList, I somehow broke it altogether while trying to add the extra step.

if (mysql_num_rows(mysql_query('SELECT * FROM runninglist WHERE firstname=\'' .$firstname . '\' AND lastname=\'' . $lastname . '\' LIMIT 1')) == 0)
{
 $sql="INSERT INTO runninglist (firstname, lastname)
 VALUES ('$_POST[firstname]','$_POST[lastname]')";
}
else
{
echo "This name has been used.";
 }

Any direction would be appreciated.

Thanx John

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql