SimpleXML不会将特定值插入db

时间:2013-11-10 19:52:41

标签: php xml xml-parsing simplexml

我有以下xml部分

<Form>
      <Team id="3041" name="Gefle" position="7" played="6" won="2" draw="3" lost="1" scored="10" against="9" points="9,00" form="D-W-D-W-D-L" />
      <Team id="3051" name="Halmstad" position="13" played="6" won="1" draw="3" lost="2" scored="7" against="9" points="6,00" form="D-W-L-D-L-D" />
</Form>

我得到的值如下:

$form=array();
for ($i = 0; $i < sizeof($veri->Sport->Country->Tournament->Season->Stage->Match->Form->Team); $i++) {
    $form['id'][$i]         = $veri->Sport->Country->Tournament->Season->Stage->Match->Form->Team[$i]['id'];
    $form['name'][$i]       = $veri->Sport->Country->Tournament->Season->Stage->Match->Form->Team[$i]['name']; 
    $form['position'][$i]   = $veri->Sport->Country->Tournament->Season->Stage->Match->Form->Team[$i]['position'];
    $form['played'][$i]     = $veri->Sport->Country->Tournament->Season->Stage->Match->Form->Team[$i]['played'];
    $form['won'][$i]        = $veri->Sport->Country->Tournament->Season->Stage->Match->Form->Team[$i]['won'];
    $form['draw'][$i]       = $veri->Sport->Country->Tournament->Season->Stage->Match->Form->Team[$i]['draw'];
    $form['lost'][$i]       = $veri->Sport->Country->Tournament->Season->Stage->Match->Form->Team[$i]['lost'];
    $form['scored'][$i]     = $veri->Sport->Country->Tournament->Season->Stage->Match->Form->Team[$i]['scored'];
    $form['against'][$i]    = $veri->Sport->Country->Tournament->Season->Stage->Match->Form->Team[$i]['against'];
    $form['points'][$i]     = $veri->Sport->Country->Tournament->Season->Stage->Match->Form->Team[$i]['points'];
    $form['form'][$i]       = $veri->Sport->Country->Tournament->Season->Stage->Match->Form->Team[$i]['form'];
//  echo $form['id'][$i] ."<br/>".$form['name'][$i] ."<br/>".$form['position'][$i]."<br/>".$form['played'][$i] ."<br/>".$form['won'][$i]."<br/>" .$form['draw'][$i]."<br/>".$form['lost'][$i]."<br/>".$form['scored'][$i] ."<br/>".$form['against'][$i] ."<br/>".$form['points'][$i] ."<br/>".$form['form'][$i]. "<hr/>"; 
}
到目前为止没问题。但是当我尝试在db中插入这些记录时:

$sorgu      = mysql_query("SELECT * FROM takim WHERE id='$homeid'");
$etki       = mysql_affected_rows();
if ($etki > 0) {
    for ($i = 0; $i < sizeof($form['id']); $i++) {
        if ($form['id'][$i] == $homeid) {
            $takiminid      = $i;
            $takimid        = $form['id'][$takiminid];
            $takimisim      = $form['isim'][$takiminid];
            $takimpozisyon  = $form['pozisyon'][$takiminid];
            $takimplayed    = $form['played'][$takiminid];
            $takimwon       = $form['won'][$takiminid];
            $takimlost      = $form['lost'][$takiminid];
            $takimdraw      = $form['draw'][$takiminid];
            $takimscored    = $form['scored'][$takiminid];
            $takimagainst   = $form['against'][$takiminid];
            $takimpoints    = $form['points'][$takiminid];
            $takimform      = $form['form'][$takiminid];
            $sorgu          = mysql_query("UPDATE takim SET isim='$takimisim', pozisyon='$takimpozisyon', oynanan='$takimplayed', kazan='$takimwon', beraber='$takimdraw', kaybet='$takimlost', scored='$takimscored', against='$takimagainst', points='$takimpoints', form='$takimform' WHERE id='$takimid'");
        }
    }
}
else { 
    for ($i = 0; $i < sizeof($form['id']); $i++) {
        if ($form['id'][$i] == $homeid) {
            $takiminid      = $i;
            $takimid        = $form['id'][$takiminid];
            $takimisim      = $form['isim'][$takiminid];
            $takimpozisyon  = $form['pozisyon'][$takiminid];
            $takimplayed    = $form['played'][$takiminid];
            $takimwon       = $form['won'][$takiminid];
            $takimlost      = $form['lost'][$takiminid];
            $takimdraw      = $form['draw'][$takiminid];
            $takimscored    = $form['scored'][$takiminid];
            $takimagainst   = $form['against'][$takiminid];
            $takimpoints    = $form['points'][$takiminid];
            $takimform      = $form['form'][$takiminid];
            $sorgu          = mysql_query("INSERT INTO takim (id, isim, pozisyon, oynanan, kazan, beraber, kaybet, scored, against, points, form) VALUES ('$takimid', '$takimisim', '$takimpozisyon', '$takimplayed', '$takimwon', '$takimdraw', '$takimlost', '$takimscored', '$takimagainst', '$takimpoints', '$takimform')");
        }
    }
}

我收到此错误:

Notice: Undefined index: id in C:\xampp\htdocs\Demo\index.php on line 154

for ($i = 0; $i < sizeof($form['id']); $i++) { in else part.

并且不会向db插入值。什么似乎是问题?

1 个答案:

答案 0 :(得分:0)

我明白了。问题是我试图比较两个对象

if ($form['id'][$i] == $homeid)

这里。为了能够成功地比较这两个是否相等,我将它们的类型设置为刚好在if语句之上的int,并且在if语句中我将它们的类型再次设置为object。所以我的解决方案很简单:

        settype($form['id'][$i], "integer");
        settype($homeid, "integer");
        if ($form['id'][$i] == $homeid) {
            settype($form['id'][$i], "object");
            settype($homeid, "object");
            $takiminid      = $i;
            $takimid        = $form['id'][$takiminid];
            $takimisim      = $form['name'][$takiminid];
            $takimpozisyon  = $form['position'][$takiminid];
            $takimplayed    = $form['played'][$takiminid];
            $takimwon       = $form['won'][$takiminid];
            $takimlost      = $form['lost'][$takiminid];
            $takimdraw      = $form['draw'][$takiminid];
            $takimscored    = $form['scored'][$takiminid];
            $takimagainst   = $form['against'][$takiminid];
            $takimpoints    = $form['points'][$takiminid];
            $takimform      = $form['form'][$takiminid];
            $sorgu          = mysql_query("UPDATE takim SET isim='$takimisim', pozisyon='$takimpozisyon', oynanan='$takimplayed', kazan='$takimwon', beraber='$takimdraw', kaybet='$takimlost', scored='$takimscored', against='$takimagainst', points='$takimpoints', form='$takimform' WHERE id='$takimid'");
        }