2013-05-20 111 views
-1

我有從XML導入數據到SQL表的問題。該XML看起來是這樣的:從XML導入數據到數據庫使用PHP循環

<MeasuringPoints> 
    <MeasuringPoint ID="BaseStation" LastChange="2013-05-20T12:15:03.837"> 
    </MeasuringPoint> 
    <MeasuringPoint ID="MiddleStation" LastChange="2012-08-01T11:47:33.160"> 
    </MeasuringPoint> 
    <MeasuringPoint ID="MountainStation" LastChange="2013-05-20T12:15:03.840> 
    </MeasuringPoint> 
</MeasuringPoints> 

使用PHP(foreach循環)

foreach($skiarea->MeasuringPoints->MeasuringPoint as $measuring) { 
    $MEASURING_POINTS = $measuring['ID']; 
    $MEASURING_LAST_CHANGE = $measuring['LastChange']; 
} 

我得到的輸出這樣的數據:

BaseStation - 1369052103 
MiddleStation - 1343821653 
MountainStation - 1369052103 

我試圖插入值batabase

mysql_query(" 
    INSERT INTO database_name SET 
    base_station_last_change = '$MEASURING_LAST_CHANGE[0]', 
    middle_station_last_change = '$MEASURING_LAST_CHANGE[1]', 
    mountain_station_last_change = '$MEASURING_LAST_CHANGE[2]', 
") OR die(mysql_error()); 

但是這段代碼沒有工作。只導入一個值。

如何將這些數據插入到sql數據庫字段?

+0

爲什麼你在插入查詢設置字段名稱????? –

回答

0
$MEASURING_LAST_CHANGE = array(); 

    foreach($skiarea->MeasuringPoints->MeasuringPoint as $measuring) { 
     $MEASURING_POINTS = $measuring['ID']; 
     $MEASURING_LAST_CHANGE[] = $measuring['LastChange']; 
    } 

    mysql_query(" 
INSERT INTO table_name (base_station_last_change,middle_station_last_change,mountain_station_last_change) VALUES (
'$MEASURING_LAST_CHANGE[0]','$MEASURING_LAST_CHANGE[1]','$MEASURING_LAST_CHANGE[2]') 
    ") OR die(mysql_error()); 
+0

這沒有奏效 - 沒有插入分貝 – Adrian

+0

我以爲你正在更新一行.. –

0
foreach($skiarea->MeasuringPoints->MeasuringPoint as $measuring) { 
    $MEASURING_POINTS[] = $measuring['ID']; 
    $MEASURING_LAST_CHANGE[] = $measuring['LastChange']; 
} 
相關問題