Rephrasing my question since it keeps getting marked as duplicate & pointing me to a question that I don't understand... can someone instead tell me which part of this is sql_api & needs to be changed??
I am trying to check my database for duplicate values before inserting new ones. I found a couple questions here asking that but couldn't find any examples where they are using PDO to connect to the database. Here is my insert statement that works fine:
if (isset($_POST['newName'])) {
$newName = trim($_POST['newName']);
if(!empty($newName)) {
$addedQuery = $db->prepare("
INSERT INTO type (typeName)
VALUES (:newName)
");
$addedQuery->execute([
'newName' => $newName
]);
}
}
This was the closest that I've gotten but it doesn't seem to work. I do have a unique requirement on the table that won't allow the duplicate values to actually be inserted but I am trying to alert the user to it. Any help is appreciated!!
if (isset($_POST['newName'])) {
$newName = trim($_POST['newName']);
$dupesql = "SELECT * FROM type WHERE typeName = :newName";
$duperaw = mysql_query($dupesql);
if($duperaw > 0) {
echo (":newName already exists")
} else {
if(!empty($newName)) {
$addedQuery = $db->prepare("
INSERT INTO type (typeName)
VALUES (:newName)
");
$addedQuery->execute([
'newName' => $newName
]);
}
}
}