我已经编写了以下代码,将xml广播中的歌曲信息放入mysql数据库中,例如:
艺术家专辑名称

它可以工作,但是如果专辑不存在,则插入是错误的,是否有办法丢失专辑,而只是插入艺术家和标题。
我的代码:

$artist = $xml->SERVERTITLE;
$title = $xml->SONGTITLE;
$pieces = explode("-", $title);

$pieces[0] = trim($pieces[0]);
$pieces[1] = trim($pieces[1]);
$pieces[2] = trim($pieces[2]);
$sql = "INSERT INTO test_xml (`title`, `album`, `artist`) VALUES ('$pieces[2]', '$pieces[1]', '$pieces[0]') ON DUPLICATE KEY UPDATE time = now(), album = VALUES(album)";

最佳答案

$artist = $xml->SERVERTITLE;
$title = $xml->SONGTITLE;
$pieces = explode("-", $title);

if ( count( $pieces ) < 3 ) {
    $pieces[0] = trim( $pieces[0] );
    $pieces[2] = trim( $pieces[1] );
    $pieces[1] = "";
} else {
    $pieces[0] = trim($pieces[0]);
    $pieces[1] = trim($pieces[1]);
    $pieces[2] = trim($pieces[2]);
}
$sql = "INSERT INTO test_xml (`title`, `album`, `artist`) "
    . "VALUES ('$pieces[2]', '$pieces[1]', '$pieces[0]') "
    . "ON DUPLICATE KEY UPDATE time = now(), album = VALUES(album)";

关于php - php爆炸功能问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33945594/

10-11 22:12