将Xml数据添加到数据库

时间:2015-10-11 13:59:50

标签: php mysql xml database

我已经让这个代码在数据库中放入一些来自xml女巫的webradio数据正在工作,问题是如果mp3文件没有专辑它会弄乱数据输入,有人可以指出一个方法,如果$ 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);

2 个答案:

答案 0 :(得分:0)

也许只需添加: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);
}

答案 1 :(得分:0)

是的,您只需要检查该值是否已设置就很容易了。

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