0

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 (paragraphs, 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'"); 
6
  • 1
    You have a column with the same name as the table? What does your DBA have to say about this? Commented Jul 6, 2011 at 21:13
  • nothing because Im just practicing..... Commented Jul 6, 2011 at 21:15
  • Are you against making a basic query against the database to see if a valid row exists and update if so? Commented Jul 6, 2011 at 21:17
  • I don't see any $url variable being instantiated? Commented Jul 6, 2011 at 21:18
  • no ...would that be fairly inefficient though? Commented Jul 6, 2011 at 21:18

2 Answers 2

1

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')
             ");
Sign up to request clarification or add additional context in comments.

4 Comments

ya it is ...how would I do that using foreach though
@Pranet: No, I think there needs to exist a UNIQUE constraint for the ON DUPLICATE KEY to work. If url is the primary key, thats's fine too.
Would finding to see if the url exists and replacing each row with the url with the new values be possible...I think thats what I'm looking for
You can use IF EXISTS ... syntax but I think it needs you to create a stored procedure and it's not the best option for many other reasons. Look here anyway: stackoverflow.com/questions/6168822/…
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.