0

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
    ]);
   }
  }
}
heat222
  • 99
  • 8
  • So, `typeName` is what you check if there is duplicate record exists? – Wolverine Jan 19 '17 at 21:13
  • @u_mulder What he says is he wants to alert the user in case the record already exists. – Wolverine Jan 19 '17 at 21:14
  • @u_mulder Are you sure this is possible duplicate of the question you linked in your comment? – Wolverine Jan 19 '17 at 21:16
  • At least I see `mysql_` api and something else with `db->prepare`, which is abloslutely __NOT__ `mysql` – u_mulder Jan 19 '17 at 21:16
  • 1
    it is a duplicate. please don't use mysql_ api, they are insecure and deprecated. – Marco Aurelio Jan 19 '17 at 21:18
  • @u_mulder Exactly. I just noticed. It's like he is using hybrid code. Using deprecated `mysql` extension for `SELECT` query and `PDO` for `INSERT` query. Strange. Lol. – Wolverine Jan 19 '17 at 21:22
  • @u_mulder I am new to SQL & PHP & do not even know what mysql_ api is. I checked the linked answer & it doesn't answer my question. If there is a different way I should be going about this, please let me know. Thanks – heat222 Jan 19 '17 at 21:22
  • @heat222 Better stick with PDO for all of your queries to database. Never use `mysql` extension since it is deprecated which means it's old and vulnerable to some security issues. – Wolverine Jan 19 '17 at 21:25
  • As you modified the question, you need to change `mysql` usage to `PDO` usage. – Wolverine Jan 19 '17 at 21:32
  • sometimes the people here get a little antsy and mark things as duplicates in a knee-jerk fashion when they dont match exactly. [the concept you might be looking for is called an 'upsert'](https://chartio.com/resources/tutorials/how-to-insert-if-row-does-not-exist-upsert-in-mysql/). – castis Jan 19 '17 at 21:35
  • you'll definitely want to read the question yours was marked as a dupe of though because that is definitely worth knowing. – castis Jan 19 '17 at 21:35
  • @castis Thank you. I asked this question because I'm new to programming & trying to figure out what is wrong with my code but rather than getting help, I get comments that my code is wrong & strange & laughed at. Real helpful. I thought this was for beginners too but I guess I need to learn more about PDO before asking any more questions. I appreciate the tips & will check out upsert as well. Thank you :) – heat222 Jan 19 '17 at 21:45
  • 1
    dont let it get you down. S.O. sees a whole lot of questions on a daily basis and sometimes some signals get mixed. you seem like you're off to a good start though, keep it up! – castis Jan 19 '17 at 21:50
  • It's not that I'm or we were laughing at you. In my comments, I was suggesting you to replace `mysql` to `PDO` to avoid using deprecated functions. That's a kind of answer I provided you. It can be either suggestion or answer. Since your question is marked as a duplicate, I can't help you provide answer with the code. – Wolverine Jan 19 '17 at 21:57
  • @Perumal93, Thanks anyway I appreciate your time. – heat222 Jan 20 '17 at 00:34

0 Answers0