在插入mysql时删除空格

时间:2015-08-02 17:56:15

标签: php mysql xml

在mysql中插入数据时,首先会出现一个空格,如何删除它?目标试图使用修剪,但所有代码都失败了。有人能给我一些线索吗?

else {

$artist = $xml->SERVERTITLE;
$title = $xml->SONGTITLE; {
$pieces = explode("-", $title);
$pieces = trim($pieces[1]);



 // performing sql query

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

$result = mysqli_query($con, $sql);

 if (!$result) {
            echo 'MYSQL ERROR';
        } else  {
        echo 'sucesso';
        }
        }
        }

3 个答案:

答案 0 :(得分:0)

删除{

$xml->SONGTITLE; { // remove it

所以你有:

$pieces = trim($pieces[1]);

现在$pieces字符串。并且$pieces[0]返回字符串的第一个符号。错误是您覆盖$pieces值并仍然认为它是一个数组。正如其他人认为的那样:

$pieces[0] = trim($pieces[0]);
$pieces[1] = trim($pieces[1]);

答案 1 :(得分:0)

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

答案 2 :(得分:0)

function trim_value(&$value) 
{ 
   $value = trim($value); 
}

array_walk($pieces, 'trim_value');