TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-05-2009, 10:04 AM   #1 (permalink)
The Wanderer
 
paulOr's Avatar
 
Join Date: Nov 2007
Location: Edinburgh
Posts: 21
Thanks: 1
paulOr is on a distinguished road
Default Funny Parse -> MySQL Insert error

Im parsing a feed, then entering the data into mysql.
When i echo $elemt->title or any of the others, it shows the correct value.

However when i insert it, its just blank....

Some help pl0x?

PHP Code:
include_once ('functions.php');
    
    
$xmlobj simplexml_load_file("http://www.colingilchrist.com/feeds/posts/default");
    
$channel $xmlobj;
    
    foreach (
$channel as $element) {
    
        
$title $element->title;
        
$content $element->content;
        
$time strtotime($element->published);
        
        echo 
$element->title;
        
        
#$check_query = mysql_query("SELECT * FROM `blog` WHERE `title` LIKE '%".$elemt->title."%'");
        #$check = mysql_num_rows($check_query);
        
        #if($check != 1) {
            
            
mysql_query("INSERT INTO `blog` (
                `user`, 
                `title`, 
                `message`, 
                `time`) VALUES(
                'Colin', 
                '"
.$element->title."', 
                '"
.$element->content."', 
                '"
.$element->published."')");
                
        
#}
    

__________________
paulOr.net
paulOr is offline  
Reply With Quote
Old 06-05-2009, 11:04 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You're attempting a loop on the data outside the <entry> node. Try modifying your loop to reflect the following, so that you're looping entry:

php Code:
foreach ($channel->entry as $element)
{

}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-05-2009, 11:38 AM   #3 (permalink)
The Wanderer
 
paulOr's Avatar
 
Join Date: Nov 2007
Location: Edinburgh
Posts: 21
Thanks: 1
paulOr is on a distinguished road
Default

nope :( doesnt return anything that way! :(
__________________
paulOr.net
paulOr is offline  
Reply With Quote
Old 06-05-2009, 11:49 AM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I don't know why but it appears to work for me when I make that change:

php Code:
$xmlobj = simplexml_load_file("http://www.colingilchrist.com/feeds/posts/default");
$channel = $xmlobj;

foreach ($channel->entry as $element) {

    $title = $element->title;
    $content = $element->content;
    $time = strtotime($element->published);
   
    #$check_query = mysql_query("SELECT * FROM `blog` WHERE `title` LIKE '%".$elemt->title."%'");
    #$check = mysql_num_rows($check_query);
   
    #if($check != 1) {
       
        $szSQL = "INSERT INTO `blog` (
            `user`,
            `title`,
            `message`,
            `time`) VALUES(
            'Colin',
            '"
.$element->title."',
            '"
.$element->content."',
            '"
.$element->published."')";
       
        echo $szSQL . '<br /><br />';
           
    #}
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-05-2009, 11:52 AM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Incidentally, you're assigning the values to new variables at the beginning of the loop, but you're not using them. Also, looking inside the commented code, you're using $elemt which won't be set at all.

I think you should be using $title, $content and $time as they're the variables you have created.

Edit: Another incidentally, if in your MySQL table you set the title column to a unique index, you won't have to use a SELECT to check if it first exists. It simply won't be entered into the MySQL table if it already exists.

One step at a time though !
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-05-2009, 11:54 AM   #6 (permalink)
The Wanderer
 
paulOr's Avatar
 
Join Date: Nov 2007
Location: Edinburgh
Posts: 21
Thanks: 1
paulOr is on a distinguished road
Default

the vars at the top were just me testing to see if it was something else, which i should of deleted ;), however element->title when echoed works fine, just when i try insert it, its blank?

I tried your one, and that too is blank, did you manage to get it in the database?
__________________
paulOr.net
paulOr is offline  
Reply With Quote
Old 06-05-2009, 11:59 AM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I see another problem that will prevent it from being entered into the database correctly. It concerns the content of the first entry (It's the only one I checked): "...it's place in business..."

If the body contains an apostrophe then it will break. You need to escape those characters:

php Code:
$content = mysql_real_escape_string($element->content);

You need to do the same for title, also. If you add the or die to your mysql_query function then you will see any further errors:

php Code:
mysql_query($szSQL) or die(mysql_error());
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Error with Pagination Orc MySQL & Databases 29 11-21-2011 10:58 AM
MySQL Server error #2003(HY000): Can't connect to MySQL Server on 'localhost'(10061) Yoosha MySQL & Databases 3 06-15-2009 02:45 PM
Keep getting mySQL error No. 1064, but i can't seem to find the problem Durux MySQL & Databases 8 04-13-2008 07:51 PM
i keep getting unknown error by mysql sarmenhb Absolute Beginners 5 02-18-2008 02:28 PM
Error in connecting to MySQL via PHP EyeDentify MySQL & Databases 0 01-03-2008 01:06 PM


All times are GMT. The time now is 07:44 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design