How to remove auto complete duplicates?

五迷三道 提交于 2020-03-05 07:06:14

问题


How do I remove the duplicates? I have 4 rows in my database table with all the same name, for example a brand, Apple, which is used 4 times, how do i remove the other duplicates with the same name? this is to avoid redundancy when inputing brands.

<script>
 $(document).ready(function(){
  $("#prod_brand").autocomplete("prod_brand_auto_complete.php", {
        selectFirst: true
  });
 });
</script>

Here is the prod_brand_autocomplete.php

<?php
 $q=$_GET['q'];
 $my_data=mysql_real_escape_string($q);
 $mysqli=mysqli_connect('localhost','root','12148qx3er','buybranded') or die("Database Error");
 $sql="SELECT prod_brand FROM inventory WHERE prod_brand LIKE '%$my_data%' ORDER BY prod_brand";
 $result = mysqli_query($mysqli,$sql) or die(mysqli_error());

 if($result)
 {
  while($row=mysqli_fetch_array($result))
  {
   echo $row['prod_brand']."\n";
  }
 }
?>

回答1:


$q = $_GET['q'];
$my_data = mysql_real_escape_string($q);
$mysqli = mysqli_connect('localhost', 'root', 'xxx', 'buybranded') or die("Database Error");
$sql = "SELECT * FROM inventory WHERE prod_brand LIKE '%$my_data%' ORDER BY prod_brand";
$result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));    
if ($result) {
    $existBrand = array();
    while ($row = mysqli_fetch_assoc($result)) {
        echo $row['prod_brand'] . "\n";
        if (in_array($row['prod_brand'], $existBrand)) {
            $delSql = "DELETE FROM buybranded.inventory WHERE id='$row[id]'";
            $delResult = mysqli_query($mysqli, $delSql) or die(mysqli_error($mysqli));
            continue;
        }
        $existBrand[] = $row['prod_brand'];
    }
}


来源:https://stackoverflow.com/questions/25173250/how-to-remove-auto-complete-duplicates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!