[英]xml foreach inserting into database multiple child nodes
我在使用XML foreach时遇到麻烦,我需要在数据库中输入多个子节点
所以对于xml部分
<Pieces>
<Piece>
<PieceNumber>1</PieceNumber>
<Depth>5</Depth>
<Width>5</Width>
<Height>5</Height>
<Weight>1</Weight>
<PackageType>PA</PackageType>
<DimWeight>0.025000</DimWeight>
<DataIdentifier>J</DataIdentifier>
<LicensePlate>JD014600002433049632</LicensePlate>
<LicensePlateBarCode>iVBORw0KGgoAAAANSUhEUgAAAZYAAABeAQMAAAA6+qC4AAAABlBMVEX///8AAABVwtN+AAAAaElEQVR42u3LIRJAQBiA0Z9AUqStextHUSTJCMRVzFDYYLiNO2whklSNM2jC9/rzRKSIymk3eaXXOqzjI5jvZnDipaLax6klUVenV5vZcDy33oj48h2Hw+FwOBwOh8PhcDgcDofzp/MC4ZwWvNbw1agAAAAASUVORK5CYII=</LicensePlateBarCode>
</Piece>
<Piece>
<PieceNumber>2</PieceNumber>
<Depth>5</Depth>
<Width>5</Width>
<Height>5</Height>
<Weight>1</Weight>
<PackageType>PA</PackageType>
<DimWeight>0.025000</DimWeight>
<DataIdentifier>J</DataIdentifier>
<LicensePlate>JD014600002433049633</LicensePlate>
<LicensePlateBarCode>iVBORw0KGgoAAAANSUhEUgAAAZYAAABeAQMAAAA6+qC4AAAABlBMVEX///8AAABVwtN+AAAAaElEQVR42u3LIRJAQBiA0X83kBRpi+A2jkKWdoSVzCpmKJLhNu6gEDVV4wya8L3+lIjYqJoOX7p0rcM6PoP5bsdNVC6mezazZObqU9ecVifFPngRLd9xOBwOh8PhcDgcDofD4XA4fzov9XcVvFOzTc8AAAAASUVORK5CYII=</LicensePlateBarCode>
</Piece>
</Pieces>
我想输入两个值
当我使用以下内容时,我两次获得相同的值
//将片段输入数据库
foreach ($xml->Pieces->children() as $plate)
{
$pl = $xml->Pieces->Piece->LicensePlate;
$query4 = "INSERT INTO pieceplates"
. " ( waybill, plate ) "
. " VALUES ('$Waybill', '$pl')";
//execute the query
$result4 = mysql_query($query4)
or die("There has been a problem entering pieces.");
$pl = $xml->Pieces->Piece->LicensePlate;
}
任何帮助表示赞赏
我假设您正在使用SimpleXML,请尝试以下操作:
$xml = simplexml_load_string($pieces_xml);
foreach ($xml as $piece)
{
$pl = $piece->LicensePlate;
$query4 = "INSERT INTO pieceplates"
. " ( waybill, plate ) "
. " VALUES ('$Waybill', '$pl')";
//execute the query
$result4 = mysql_query($query4)
or die("There has been a problem entering pieces.");
}
mysql_query()
顺便说一句。 自PHP 5.5起已弃用,应替换为mysqli_query()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.