向数据库添加Xml数据


Add Xml data to database

我已经使这段代码放入数据库中一些来自xml的web广播数据正在工作,问题是如果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);

不妨加上: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);
}

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

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