Jump to content
sonu

PHP & MYSQL Help

Recommended Posts

Hi Guys! I have 2 fields name & email in mysql database.I want to check if the record exists or not in the DB and if not then it would insert records in the database. Quick help really appreciated. Thanks!

Share this post


Link to post
Share on other sites

does your field have any unique column or primary key column?

Share this post


Link to post
Share on other sites

$name;$email;$query = mysql_query("select * from table_name where name=$name and email=$email");$num_of_row=mysql_num_rows($query);if($num_of_row <= 0){$another_query=mysql_query("insert into table_name (name, email) values ($name, $email)");}

Share this post


Link to post
Share on other sites

You dont need to send a query(reduce overhead) if you have any PK or unique constraint in your database table. if your database schema wants any unique column you should constraint it from the database. you can directly execute the INSERT, if there is no any duplication it will get inserted. otherwise when it fails it sends error, you check the specific error number using mysql_errno() and you can show any specific custom error on that. you can check list of mysql error number here ==> http://dev.mysql.com...ges-server.htmlhttp://php.net/mysql_errno

Edited by birbal

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...