我有一個工作代碼插入數據foreach循環兩次與多個陣列
<?php
$con = mysql_connect('localhost','test','test');
mysql_select_db('test',$con);
require_once("xml2json.php");
$testXmlFile = 'myxml.xml';
$xmlStringContents = file_get_contents($testXmlFile);
$jsonContents = "";
$jsonContents = xml2json::transformXmlStringToJson($xmlStringContents);
$obj =json_decode($jsonContents);
$rows = array();
foreach($obj->rss->channel->item as $item) {
$rows[] = "('".mysql_real_escape_string($item->title)."','".mysql_real_escape_string($item->description)."')";
}
$del_horoscope = "delete from jos_horoscope";
mysql_query($del_horoscope);
$query = "INSERT INTO `jos_horoscope` (`title`,`description`) VALUES ".implode(', ',$rows);
print_r ($query);
mysql_query($query);
// Do the query - do NOT show the output of mysql_error() in a production environment!
if (!mysql_query($query)) echo 'Oh no! Something went wrong with the query: '.mysql_error();
?>
但數據與該腳本的每次運行插入兩次。我在使用刪除語句刪除數據之前插入新的one.Can任何人請幫助我解決這個問題。這裏是兩次數據填充的屏幕截圖。 http://i.imgur.com/7IM1mOE.png?1
新的編輯
這裏是INSERT QUERY回聲http://pastebin.com/btZ7f85a 的數據不重複。
在此先感謝
你不必通過這個例子的外觀工作代碼,你忘了關關密碼字符串在'的mysql_connect()';) – 2013-04-26 02:32:19
是數據的XML複製1)? 2)在json? 3)在foreach循環? – Patashu 2013-04-26 02:32:44
@DarylGill其實在發佈這裏之前有一個錯字錯誤。謝謝我現在編輯它。 :) – Yogus 2013-04-26 02:35:28