TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Funny Parse -> MySQL Insert error (http://www.talkphp.com/general/4481-funny-parse-mysql-insert-error.html)

paulOr 06-05-2009 10:04 AM

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."')");
                
        
#}
    



Wildhoney 06-05-2009 11:04 AM

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)
{

}

paulOr 06-05-2009 11:38 AM

nope :( doesnt return anything that way! :(

Wildhoney 06-05-2009 11:49 AM

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 />';
           
    #}
}

Wildhoney 06-05-2009 11:52 AM

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 :-) !

paulOr 06-05-2009 11:54 AM

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?

Wildhoney 06-05-2009 11:59 AM

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());


All times are GMT. The time now is 02:19 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0