SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Field wont insert into the database

    Having trouble inserting some data into a database, basically i'm scraping data from an RSS feed and looping through each entry and inserting it into a database but when it gets to the 'content' field it fails, but there is no errors, it just wont insert.. if i remove the content field then it inserts just fine so i think thats the culprit.

    PHP Code:

    $RSS_DOC 
    simpleXML_load_file($feed_url);


    foreach(
    $RSS_DOC->channel->item as $RSSitem) {

        
    $content $RSSitem->description;

        
    $item_insert_sql "INSERT INTO default_posts(content) VALUES ('$content')";

        
    $insert_item mysql_query($item_insert_sql$db);


    $RSSitem->description would be:

    Code:
    <description><![CDATA[Madonna celebrated Yom Kippur in New York City on Tuesday (25.09.12). <BR>
    The 'Celebration' singer is a devotee of Jewish offshoot Kabbalah and joined with other followers of the religion to celebrate the Day of Atonement, the holiest day in the calendar for Jewish people.<BR>
    Madonna, 54, arrived at the city's Park Avenue Armory wearing a bright purple sweat suit with the number 86 on them and a hat with the word 'Vogue' written on it. <BR>
    A source told the New York Post newspaper: &quot;Madonna was the last to arrive, and it seemed like they were holding up the ceremony to wait for her. <BR>
    &quot;She came through a back entrance with her daughter, Lourdes, and was seated in the front row. Once she was seated, it could begin. All the men were in white but Madonna had a loud track suit on. Also there was her younger boyfriend [Brahim Zaibat], who arrived wearing cream.&quot; <BR>
    While her entrance may have caused a stir, the 'Hung Up' singer certainly focused during the service, and appeared oblivious to those around her, including fashion designer Donna Karan, who was seated nearby. <BR>
    The witness added: &quot;[Madonna] definitely set herself apart from everyone else. She is like the queen of Kabbalah.&quot;<BR>
    A statement on the Kabbalah website explains the religious day's significance, saying &quot;[During] Yom Kippur, we sit on the throne with Binah and remove the fog in our lives for the entire year to come.&quot; <BR>
    Earlier this week, Madonna was forced to clarify comments she made at a concert on Monday (24.09.12) in Washington, where she referred to the US president as a &quot;black Muslim,&quot; despite him being openly Christian.<BR>
    Madonna said she was &quot;being ironic,&quot; adding: &quot;Yes, I know Obama is not a Muslim, though I know that plenty of people in this country think he is.&quot;]]></description>
    if i $echo description; it prints correct so i know it's pulling the data but why wont it go into to the DB? the $content field is set to LONGTEXT.

    Does it have anything to do with <![CDATA[ ?

    Any suggestions?

  2. #2
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, ive worked out it's if the content contains an apostrophe or quotes.

    is there an easy way to fix this instead of using lots of str_replace?

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,815
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    mysql_real_escape_string, look up that function and you are set.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,604
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    The other alternative is to use PDO or mysqli where you can use separate prepare and bind statements in place of the query statement. That way you'd keep the sql and data separate and avoid the two being jumbled together with the potential for quotes in the data to mess things up.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •