Jump to content

Database Insertion Not Working


spec36

Recommended Posts

I'm trying to learn writing php scripts with html-simple-dom. I am scrapping my website with html-simple-dom and the below code, but I am running into an issue with inserting the scrapped data into my database.

 

This code scraps the website and grabs the html data I want.

 

$ret =  $html->find('table[class=data] tr');
foreach($ret as $visitor){
$visitor =  $visitor->find('td','1') . "<br>";
$insert="INSERT INTO $dbtable (visitor) VALUES ('$visitor')";
mysql_query($insert) OR die(mysql_error());
}

 

But this is being inserted into the database "visitor" field.

 

<td colspan="1" rowspan="1" style="text-align: left;"><a style="border-bottom:1px dotted;" onclick="loadTeamSpotlight(jQuery(this));" rel="HEN" href="javascript:void(0);">HENRY</a></td><br>

 

I only want "HENRY" to be extracted out of the html code above and inserted into the database table.

 

Any help would be greatly appreciated, as I have been pulling my hair out trying to figure this out. Do I need to use explode or something?

 

Thanks,

Link to comment
https://forums.phpfreaks.com/topic/248531-database-insertion-not-working/
Share on other sites

if the insert always follows that formatting,

$ret =  $html->find('table[class=data] tr');
foreach($ret as $visitor){
$visitor =  $visitor->find('td','1') . "<br>";
$replaceArray=array('<td colspan="1" rowspan="1" style="text-align: left;"><a style="border-bottom:1px dotted;" onclick="loadTeamSpotlight(jQuery(this));" rel="HEN" href="javascript:void(0);">','</a></td><br>');
$visitor=str_replace($replaceArray,'',$visitor);
$insert="INSERT INTO $dbtable (visitor) VALUES ('$visitor')";
mysql_query($insert) OR die(mysql_error());
}

 

##TEST##
$string='<td colspan="1" rowspan="1" style="text-align: left;"><a style="border-bottom:1px dotted;" onclick="loadTeamSpotlight(jQuery(this));" rel="HEN" href="javascript:void(0);">HENRY</a></td><br>';
$replaceArray=array('<td colspan="1" rowspan="1" style="text-align: left;"><a style="border-bottom:1px dotted;" onclick="loadTeamSpotlight(jQuery(this));" rel="HEN" href="javascript:void(0);">','</a></td><br>');
$string=str_replace($replaceArray,'',$string);
echo $string;

I would go one layer deeper where you are looking for 'td' and actually look for the 'a' tag.  Then you would be able to grab the innertext.

 

/* An extension to your code */
$visitor =  $visitor->find('td','1')->find('a','1')->innertext . "<br>";

 

~juddster

Thank you so much for the help guys/girls.

 

When I run this code:

 

/* An extension to your code */
$visitor =  $visitor->find('td','1')->find('a','1')->innertext . "<br>";

 

I receive this error message when running the script

 

Fatal error: Call to a member function find() on a non-object in C:\Program Files\EasyPHP-5.3.8.0\www\nhl\index.php on line 42

 

This error seems to be happening , because of the extra ->find and ->innertext that was added to the statement. I didn't know you could extend the statement like that, it is very cool, but must need to be written differently. Any ideas?

Thanks for the info Joel. Your code gave me a clear idea about how to go about this, but turns out the database has two inserts before that html I posted that contains <br>. So two <br>'s are posted to the table and then all the other records follow what I posted.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.