【问题标题】:Add Xml data to database将 Xml 数据添加到数据库
【发布时间】:2015-10-11 13:59:49
【问题描述】:

我已经编写了这段代码来将一些来自 xml 女巫的 webradio 数据放入数据库中,问题是如果 mp3 文件没有专辑,它会弄乱数据输入,有人可以指出 if $pieces[ 1]存在运行代码并添加相册如果没有不做任何事情,并将其他的添加到正确的位置。

$title = $xml->SONGTITLE;
$pieces = explode("-", $title);
$pieces[0] = trim($pieces[0]);
$pieces[1] = trim($pieces[1]);
$pieces[2] = trim($pieces[2]);
// performing sql query
$sql = "INSERT INTO test_xml (`title`, `album`, `artist`) VALUES  ('$pieces[2]', '$pieces[1]', '$pieces[0]') ON DUPLICATE KEY UPDATE time =   now(), album = VALUES(album)";
$result = mysqli_query($con, $sql);

【问题讨论】:

  • 我不得不对这个问题投反对票,因为答案很明显,你甚至在你的问题中说了如何去做。
  • 约翰,我卡住了,我只是想要一些想法,我是 php 新手。
  • 对不起,伙计们,我无法正常工作。

标签: php mysql xml database


【解决方案1】:

也许只需添加:if ($pieces[1]!="")

$title = $xml->SONGTITLE;
$pieces = explode("-", $title);
$pieces[0] = trim($pieces[0]);
$pieces[1] = trim($pieces[1]);
$pieces[2] = trim($pieces[2]);
if ($pieces[1]!="")
{
// performing sql query
$sql = "INSERT INTO test_xml (`title`, `album`, `artist`) VALUES  ('$pieces[2]', '$pieces[1]', '$pieces[0]') ON DUPLICATE KEY UPDATE time =   now(), album = VALUES(album)";
$result = mysqli_query($con, $sql);
}

【讨论】:

  • 问题是当没有$pieces[1]时,$pieces[2]就是$pieces[1]。 i.imgur.com/D5Kcbki.png
  • 我明白了。 if (count($pieces)<3) 也是如此
  • 我已将问题简化为:如果 $pieces[2] 不存在,则会给出错误“注意:未定义偏移量:2”
  • if(isset($pieces[2]))也是如此
【解决方案2】:

是的,很简单,您只需要检查值是否已设置。

if(isset($value_to_check))
{
    //true - your item has a value.
}
else
{
    //do somthing else.
}

【讨论】:

    猜你喜欢
    • 2019-10-19
    • 2018-01-22
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 2020-04-04
    相关资源
    最近更新 更多