开发者

mysql update table if column exists (foreach)

I've been playing with this :

foreach($textnode as $key => $value) {

$value = stripslashes($value);
 $value = mysql_real_escape_string($value, $con);

 mysql_query("INSERT INTO paragraphs (paragrap开发者_StackOverflow社区hs, url)
 VALUES ('$value', '$url')");



}

I've been trying to update the column "paragraphs" for where the url already exists

This doesn't seem to work well as it just replaces each row in paragraphs with the 1st paragraph. (repeats it over and over again)

mysql_query("UPDATE paragraphs SET paragraphs = '$value'
 WHERE url = '$url'"); 


Is the url field unique?

If not, add a UNIQUE constraint on it and use INSERT INTO . . . ON DUPLICATE KEY UPDATE with something like this:

 mysql_query("INSERT INTO paragraphs (paragraphs, url)
                VALUES ('$value', '$url')
              ON DUPLICATE KEY
                UPDATE paragraphs = '$value'
             ");

or (do you want to "append" the new '$value' when url already exists?):

 mysql_query("INSERT INTO paragraphs (paragraphs, url)
                VALUES ('$value', '$url')
              ON DUPLICATE KEY
                UPDATE paragraphs = CONCAT(paragraphs, '$value')
             ");


Try running the query

SELECT paragraphs, url
FROM   paragraphs;

to see what your table really contains. It may be that you've put in all the same URLs into your table. Which would explain the update behavior.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜