0

Im building a login for our employees and now I'm creating the validation for each input fields. If I have a table named pages and inside that table I've got a column field named title. As an example I've created a dummy title My new title.

I've created a function that let's our users create new pages, and I want to be able to check if the title already exists in our database.

For example, if the user types in My new title and submits the form, then he gets an error message with the following text: Sorry, your title already exists

I know I haven't provided much information here, but I'm from scratch on this one. I know how to check if a number is the same, or if two input field/db rows doesn't match each other. It may be an easy one, but I really can't figure it out.

I would be very grateful if you had a simple solution to this. Continue a wonderful day :)

dinkode.dk
  • 57
  • 1
  • 12
  • Yes, could be. But like I said I'm from scratch. I don't know so much about `**INSERT** ... **ON DUPLICATE**` – dinkode.dk Feb 16 '14 at 12:21

2 Answers2

2

You can achieve this by these way:

  1. Add unique constrain to column title
  2. Using INSERT ... ON DUPLICATE KEY UPDATE statement
  3. Do a query before insert on that column value if it return more than 0 result than it exist. like : SELECT * FROM pages WHERE title = 'new_title';
Nishant Kumar
  • 2,199
  • 2
  • 22
  • 43
0

Solutions would depend on which side your validation is on

Client side :

I would suggest using jquery validate which has an option to validate using a ajax call, the function that the ajax would call would be something like below

Server side:

I would create a function that queries the pages table looking for the posted page title, if the query returns no results then it is safe to continue, else the title exists and you can display the error

the query would be something like

SELECT title FROM page WHERE title = $_POST['title']
user1652319
  • 103
  • 1
  • 10