0

model.php

function view_prod_cate()
{
    global $conn;
    $sql = ("SELECT * FROM `tblproductcategory` order by category_name");
    $res = $conn->prepare($sql);
    $res->execute();
    return $res;
}

function add_prod_cate($add_prod_cate){
    global $conn;
    $sql=("insert into `tblproductcategory`(`category_name`) values('$add_prod_cate')");
    $pres=$conn->prepare($sql);
    $pres->bindValue(1,$add_prod_cate);
    $pres->execute();
    return $pres;
}

controller.php

if (isset($_POST['btn_add_prod_cate'])){
            $add_prod_cate = add_prod_cate(@$_POST['add_prod_cate']);
            if ($add_prod_cate) {
                echo <a>New record added!</a>
        
            } else {
                echo <a>failed to add</a>;
            }
}

using PDO, how to avoid duplicate record before inserting to mysql. please show me some tips. thankyou.

Sadat
  • 1
  • 5
  • Check whether it is already there, before you insert? – KIKO Software Jun 05 '22 at 09:21
  • 1
    Just copy your title into google and follow any of the _**many**_ answers/guides/tutorials that shows you how? [SO isn't a substitute for doing your own research.](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – M. Eriksson Jun 05 '22 at 09:22
  • Why not just add a unique key onto the column you do not want to be duplicated and then use `PDOException::getCode` to match it to `1062` or/and `23000` (which is duplicate error) then do what you like code wise – Jaquarh Jun 05 '22 at 09:25
  • @parveen, yes to be inserted UNIQUE – Sadat Jun 05 '22 at 09:30

0 Answers0