[英]Copy column values to another column in the same table
I'm trying to copy title column to keywords column in database, so the keywords will be inserted automatically from the title. 我正在尝试将标题列复制到数据库中的关键字列,因此将自动从标题中插入关键字。
http://store2.up-00.com/2015-06/1435609110941.png http://store2.up-00.com/2015-06/1435609110941.png
I want to add comma ', ' before each word for example. 例如,我想在每个单词前添加逗号','。
" It's my first program "
it will turn into 它会变成
" It's, my, first, program, "
This the code I wrote. 这是我写的代码。
<?php
// $id =mysql_insert_id;
$select_posts = mysql_query("SELECT * FROM `posts`");
while($row = mysql_fetch_array($select_posts)){
$id = $row['post_id'];
$text = $row['post_title'];
$delim = ' \n\t,.!?:;';
$tok = strtok($text, $delim);
while ( $tok !== false){
echo $tok1 = $tok.',';
mysql_query("UPDATE `posts` SET `post_keywords` = '$tok1' WHERE `post_id` = $id ");
$tok = strtok($delim);
}
}
?>
it insert the last word in each title column , because the words is overwritten by while loop. 它在每个标题列中插入最后一个单词,因为单词被while循环覆盖。
Please help me . 请帮我 。
Concat the values: 合并值:
... SET post_keywords = CONCAT(post_keywords, '$tok1')
and note that you're vulnerable to sql injection attacks . 并请注意,您容易受到sql注入攻击的攻击 。 Just because that $tok1 value came out of a database doesn't mean it's safe to REUSE in a query...
仅仅因为$ tok1值是从数据库中发出的,并不意味着在查询中重用是安全的...
您可以通过一个查询来完成:
UPDATE `posts` SET post_keywords = REPLACE(post_title, ' ', ',');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.