0

I have the following in my PHP.

$stmt = $conn->prepare("INSERT IGNORE INTO savesearch (user, searchedFor, sortOrder, buildURLString, aspectFilters, oneSignalId, totalEntries)
  VALUES (?, ?, ?, ?, ?, ?, ?)");
  $stmt->bind_param("sssssss", $user, $searchedFor, $sortOrder, $buildURLString, $aspectFilters, $oneSignalId, $totalEntries);

  // set parameters and execute
  $user = $_POST['user'];
  $searchedFor = $_POST["searchedFor"];
  $sortOrder = $_POST["sortOrder"];
  $buildURLString = $_POST["buildURLString"];
  $aspectFilters = $_POST["aspectFilters"];
  $oneSignalId = $_POST["oneSignalId"];
  $totalEntries = $_POST["totalEntries"];

  if ($stmt->execute()) {
    $output->success = true;
    echo json_encode($output);
  } else {
    $error->error = mysqli_error($conn);
    echo json_encode($error);
  }

However, IGNORE is not being picked up, it continues to add entries. Is there another good way to fix this?

Id like to see if the USER and the URL is the same, dont add, echo duplicate entry.

4
  • Use unique constraint mysqltutorial.org/mysql-unique Commented Dec 17, 2018 at 7:09
  • Have you indexed the particular column, for which you want to avoid duplicates as PK? Commented Dec 17, 2018 at 7:09
  • make unique attribute in database..and check affected rows ..if any row affected then send success response Commented Dec 17, 2018 at 7:11
  • Use Unique key constraints and you also validate duplicate check validation from frontend. follow this w3schools.com/sql/sql_unique.asp. Commented Dec 17, 2018 at 7:12

1 Answer 1

1

IGNORE is actually mostly for the opposite of what you want here. Instead, you can amend your MySQL table something like:

ALTER TABLE savesearch ADD UNIQUE KEY(user, buildURLString)

Then remove your IGNORE keyword

Sign up to request clarification or add additional context in comments.

2 Comments

ok i was able to add those as unique. in PHP, how would i echo that out?
thanks for the tip. for anyone else seeing this, this is what I needed for the SQL. CREATE UNIQUE INDEX WHATEVER_HERE ON tbl_name(row_name,row_name);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.