• 0

Field wont insert into the database


Question

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.


$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:

<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: "Madonna was the last to arrive, and it seemed like they were holding up the ceremony to wait for her. <BR>
"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." <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: "[Madonna] definitely set herself apart from everyone else. She is like the queen of Kabbalah."<BR>
A statement on the Kabbalah website explains the religious day's significance, saying "[During] Yom Kippur, we sit on the throne with Binah and remove the fog in our lives for the entire year to come." <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 "black Muslim," despite him being openly Christian.<BR>
Madonna said she was "being ironic," adding: "Yes, I know Obama is not a Muslim, though I know that plenty of people in this country think he is."]]></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?

sorry about double post neowin borked

Link to comment
https://www.neowin.net/forum/topic/1108587-field-wont-insert-into-the-database/
Share on other sites

1 answer to this question

Recommended Posts

  • 0

You have forgotten the rule zero - to escape text data before passing to MySQL. Your query becomes malformed like this:

INSERT INTO default_posts(content) VALUES ('<description><![CDATA[Madonna celebrated Yom Kippur in New York City on Tuesday (25.09.12). <BR> The ');

SQL thinks your text data ends at the first single quote it sees. However, it then finds more text that totally isn't valid SQL statement. And so INSERT fails. Of course, if you don't check for errors, it won't show any.

Solution (partly):

A) $content = mysql_real_escape_string($RSSitem->description);

B) Use mysqli and its parameter binding/binary transfer. Escape text data anyway (with mysqli_real_escape_string(), accordingly) - to avoid problems outputting it later.

This topic is now closed to further replies.
  • Posts

    • I agree when are you going to read this (really poor BTW) article?
    • I disagree; they come off very "bitchy" and "whiny". Make a great product and combine that with a great price (free) and people will come over to your side. Or build it and they will come as they say. Constantly trying to get attention by complaining all the time, will turn people off to your product.
    • It use to be a nightmare, with LibreOffice supporting a newer draft ODF standard by default, and Microsoft Office supporting the older non-draft standard. Now that they both support the same version of ODF, they should be interoperable.
    • Brave Browser 1.91.171 by Razvan Serea Brave Browser is a lightning-fast, secure web browser that stands out from the competition with its focus on privacy, security, and speed. With features like HTTPS Everywhere and built-in tracker blocking, Brave keeps your online activities safe from prying eyes. Brave is one of the safest browsers on the market today. It blocks third-party data storage. It protects from browser fingerprinting. And it does all this by default. Speed - Brave is built on Chromium, the same technology that powers Google Chrome, and is optimized for speed, providing a fast and responsive browsing experience. Brave Browser also features Brave Rewards, a system that rewards users with Basic Attention Tokens (BAT) for viewing opt-in ads. This innovative system provides an alternative revenue model for content creators and a way to support the Brave community. SlimBrave Neo takes all the good things about Brave and makes them even better by keeping everything clean, light, and privacy-focused. It removes the extra clutter, turns off features you might not need, and cuts down on anything that could slow you down or collect unnecessary data. Because it relies on simple settings and policies instead of modifying the browser itself, you still get full Brave compatibility—just in a smoother, lighter, and more privacy-friendly package. Brave Browser 1.91.171 changelog: General Fixed Cardano not being disabled on upgrade to Brave Origin. Upgraded Chromium to 149.0.7827.103. Origin Removed “Survey Panelist” setting from brave://settings/privacy. Fixed P3A and usage ping under brave://settings/privacy being displayed on first launch on Linux. Upgraded Chromium to 149.0.7827.103. Download: Brave Browser 64-bit | 1.2 MB (Freeware) Download: Brave Browser 32-bit View: Brave Homepage | Offline Installers | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Hi. As the title suggests, I can't access the forum on my phone. I'm using Edge on Android and when I try to navigate to the forum I get a "we value your privacy" popup and none of the buttons are clickable. It effectively stonewalls me from reading any forum content.
  • Recent Achievements

    • Rookie
      Marzoid went up a rank
      Rookie
    • Community Regular
      coch went up a rank
      Community Regular
    • One Year In
      slackerzz earned a badge
      One Year In
    • One Year In
      highriskpaym earned a badge
      One Year In
    • One Month Later
      highriskpaym earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      519
    2. 2
      PsYcHoKiLLa
      190
    3. 3
      +Edouard
      156
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      75
  • Tell a friend

    Love Neowin? Tell a friend!