我每天都有几个要插入Mysql数据库的项目。数据包含重复记录我只想插入不需要重复项目的不同记录。我已经这样做但是当我第二天运行代码时,它会用新的代码替换数据。我不想丢失以前的数据。我需要先前和新的数据,两天都没有重复数据。
有人这样工作吗?提前致谢
<?php
set_time_limit(0);
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "crawl";
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to database");
mysql_select_db($dbname, $conn);
$product_titles = array();
$q1 = mysql_query("SELECT * FROM `product-title`");
while($res1 = mysql_fetch_assoc($q1))
{
$product_titles[] = $res1['title'];
}
$q2 = mysql_query("SELECT * FROM `product-list-url`");
while($res2 = mysql_fetch_assoc($q2))
{
$url = $res2['url'];
$html = file_get_contents($url);
$doc = new DOMDocument();
@$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
foreach($product_titles as $product_title)
{
$found = $xpath->evaluate("boolean(//span[contains(text(), '$product_title')])");
if($found == false)
{
//echo "Not Found";
}
if($found == true)
{
$elements = $xpath->evaluate("//span[contains(text(), '$product_title' )]/following-sibling::div/span[@class='list_sale_price']");
if (!is_null($elements))
{
foreach ($elements as $element)
{
$nodes = $element->childNodes;
foreach ($nodes as $node)
{
$price = $node->nodeValue;
$price1 = preg_replace('/[^0-9-.]/','',$price);
$date = date('y-m-d');
mysql_query("INSERT IGNORE INTO `prices` (`url`, `ptitle`, `price`, `date`) VALUES ('$url','$product_title', '$price1', '$date')") or die(mysql_error()); }
}
}
}
}
}
}
?>