如何删除自动完成重复?

时间:2014-08-07 02:28:01

标签: php forms select autocomplete

如何删除重复项?我的数据库表中有4行具有相同的名称,例如品牌,Apple,使用了4次,如何删除其他具有相同名称的重复项?这是为了避免在输入品牌时出现冗余。

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

这是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 个答案:

答案 0 :(得分:0)

$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'];
    }
}