【问题标题】:Prevent duplicate entry in php [duplicate]防止php中的重复条目[重复]
【发布时间】:2013-12-10 04:55:03
【问题描述】:

我想防止重复输入。当我输入已经存在的相同名称时它将起作用,但如果我将其更改为小写字母,它将不起作用。例如已经存在的数据是:开发者。如果我输入开发者它给出消息已经存在但我输入开发者它将插入到数据库中。

我的代码如下:

  function insert_skills()
  {
      extract($_POST);
      $user_id = $_SESSION['user_id'];
      $Qry = mysql_query("select `role_id` from `tbl_job_role` where`industry_id`='$industry_id' AND `funarea_id`='$funarea_id' AND `role_name`='$job_role'") or die(mysql_error());
      if(mysql_num_rows($Qry)>0)
      {
           echo "Already Exist!";
      }
      else
      {
              mysql_query("insert into `tbl_job_role`(`industry_id`,`funarea_id`,`role_name`,`created_by`,`created_on`) values('$industry_id','$funarea_id','$job_role','$user_id',NOW())") or die(mysql_error());
              setcookie("msg","Successfully Inserted.",time()+5);
       }
    }

【问题讨论】:

  • 开发者和开发者不同,尽量使用区分大小写的代码

标签: php


【解决方案1】:

您好,请使用 LOWER 函数

LOWER(`Value`) = LOWER("value")

类似的东西

"select `role_id` from `tbl_job_role` where`industry_id`='$industry_id' AND `funarea_id`='$funarea_id' AND LOWER(`role_name`) = LOWER('$job_role')"

【讨论】:

    【解决方案2】:

    你最好使用LIKE

    $Qry = mysql_query("select `role_id` from `tbl_job_role`
                        where`industry_id`='$industry_id' 
                           AND `funarea_id`='$funarea_id' 
                           AND `role_name` LIKE '$job_role'") or die(mysql_error());
    

    【讨论】:

    • LIKE 使用区分大小写的匹配。在这种情况下不建议使用
    【解决方案3】:

    在您的查询中使用iLIKE。它看起来像

    $Qry = mysql_query("select `role_id` from `tbl_job_role` where`industry_id`='$industry_id' AND `funarea_id`='$funarea_id' AND `role_name` iLIKE '$job_role'") or die(mysql_error());
    

    如果解决了您的问题,请标记为答案。

    【讨论】:

      猜你喜欢
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 1970-01-01
      • 2012-02-07
      相关资源
      最近更新 更多