0

我有一个小问题需要你的帮助。如何更新以逗号分隔的部分文本组。当文本输入数据库时​​,它是一行文本,每个单词之间有逗号,但是当我想将它回显时,我使用explode函数将它们分成单独的单词,而不是像我这样的单行文本它在数据库中。

所以我现在的问题是,如何对数据库中的单个单词进行更新/删除。请记住,我将它作为数据库中的单行文本,我对更新整行文本不感兴趣......只是文本中的一个单词..

谢谢。

$text = "name,fname,lname,class,age" ;    
$newtext = explode(",", $text) ;
4

2 回答 2

0

从数组中更新或删除单词,然后将数组内爆并将新值保存到数据库字段中。

//Remove all instances of $value from $array
function array_remove($array, $value) {
  return array_filter($array, function ($e) use ($value) {
      return $e != $value;
    });
}

// Add only unique values
function array_add($array, $value) {
  if (!in_array($value, $array))
    $array[] = $value;
  return $array;
}

$text = "name,fname,lname,class,age" ;
$newtext = explode(",", $text) ;
$newtext = array_remove($newtext, 'lname'); // Remove lname
$newtext = array_add($newtext, 'mail');     // Add mail
$newtext = array_add($newtext, 'class');    // Won't add it again
$newtext = implode(',', $newtext);          // name,fname,class,age,mail
于 2012-06-18T08:16:56.617 回答
0

可以在 sql 更新查询中使用 replace() 函数。

Update table SET text=REPLACE(text,'age','newstring')
于 2012-06-18T08:09:56.377 回答