Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Can I have javascript and php work together to check if an entry already exists in the database?

I have a form that submits new data into a database, but I would like the page to notify the user if their entry is already entered before they click submit. For example if they type in a certain username into the form, can javascript tell them that the user name already exists before clicking submit? If so, how do you do it? Thanks

2 Answers

Relevance
  • Anonymous
    1 decade ago
    Favorite Answer

    Yes, it's possible. Look into the Ajax (Asynchronous JavaScript And XML) technology.

  • 5 years ago

    When the database isn't finding the '$name' value in your field it is returning 0 rows. I bet your php code assumes that you are getting at least one row back. Hence the error when the resource doesn't have anything in it. There are two ways around it. Either you can modify your sql to $query = "SELECT count(*) FROM users WHERE name='$name';"; which will guarantee you a result where you can just check that it's greater than 0 or you can use the mysql_num_rows() function to check that you actually have a row before you fetch the first row e.g. // does user exist? $name = mysql_real_escape_string($name); $query = "SELECT * FROM users WHERE name='$name';"; $res = mysql_query($query); if (mysql_num_rows($res) > 0) { // yes, pull in the user details } else // no, user doesn't exist }

Still have questions? Get your answers by asking now.