如何删除自动完成的重复项? [英] How to remove auto complete duplicates?
本文介绍了如何删除自动完成的重复项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何删除重复项?我的数据库表中有4行具有相同的名称,例如一个品牌Apple,使用了4次,如何删除其他具有相同名称的重复项?这是为了避免在输入品牌时出现冗余.
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>
这是prod_brand_autocomplete.php
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";
}
}
?>
推荐答案
$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'];
}
}
这篇关于如何删除自动完成的重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文