Jump to content

Integrity constraint violation: 1062 Duplicate entry


ZionBludd
 Share

Recommended Posts

Hi guys, I am wondering how I deal with this error message. Whenever I am trying to save a page I am getting this error when someone enters the same name in the "name" textbox. 

 

Here is my code 

<?php	if ($input->post->reviews || $input->post->name) {

						  // Save in the ProcessWire page tree; map submission to the template fields
						  $np = new Page(); // create new page object
						  $np->template = $templates->get("reviews");
						  $np->parent = $pages->get("/reviews/");

						  $name = $sanitizer->text($input->post->name);
						  $email = $sanitizer->text ($input->post->email);
						  $review = $sanitizer->text ($input->post->review);

						  // Match up the sanitized inputs we just got with the template fields
						  $np->of(false);
						  $np->title = $name;
						  $np->page_id = $page->id;
						  $np->name = $name;
						  $np->email = $email;
						  $np->review = $review;
						  $np->save();
						}
						?>

 

Link to comment
Share on other sites

Simply don't set a name, then PW will take the title you assigned and create a unique name from it by running it through $sanitizer->pageName and appending an incremental suffix if the name has already been taken. There can only be one page with a given name under the same parent. You can read some more about it in this discussion:

 

Link to comment
Share on other sites

1. Use the right sanitizer

$email = $sanitizer->email($email);
$pagename = $sanitizer->pageName($pagename); // OR
$pagename = $sanitizer->pageNameUTF8($pagename);

2. Set adjustName to true in the save options array to make your page name unique

$np = new Page()
// ...
$np->save(array('adjustName' => true));

 

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...