Page 1 of 1
Problem inserting into database
Posted: Sat Mar 11, 2006 9:00 am
by GeXus
Hello Everyone!
I am trying to insert some text into a database. Basically I am looping through an RSS feed and inserting parts of it into a database. One of the variables is called title, when I echo the title, it seems to be printing out just fine, and when I put a title into the insert statement manually, it inserts it just fine... but when I use the variable in the insert, it simply is null...... no idea why!
Any ideas??
Thanks alot!
Posted: Sat Mar 11, 2006 9:02 am
by s.dot
could you please post your code for us to look at
Posted: Sat Mar 11, 2006 9:05 am
by GeXus
feyd | Please use Code: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Code: Select all
<?php
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $title, $ASIN, $link;
if ($insideitem) {
$tag = $name;
} elseif ($name == "ITEM") {
$insideitem = true;
}
}
function endElement($parser, $name) {
global $insideitem, $tag, $title, $ASIN, $link;
if ($name == "ITEM") {
printf("<dt><b><a href='%s'>%s</a></b></dt>",
trim($link),htmlspecialchars(trim($title)));
printf("<dd>%s</dd>",htmlspecialchars(trim($ASIN)));
$title = "";
$ASIN = "";
$link = "";
$insideitem = false;
}
}
function characterData($parser, $data) {
global $insideitem, $tag, $title, $ASIN, $link;
if ($insideitem) {
switch ($tag) {
case "TITLE":
$title .= $data;
break;
case "ASIN":
$ASIN .= $data;
break;
case "LINK":
$link .= $data;
break;
}
$user="root";
$password="";
$database="amazon";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$title = str_replace("'","\'","$title");
$query="INSERT INTO dvds (ASIN, TITLE) VALUES ('$ASIN','$title')";
mysql_query($query);
echo $title;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=0Z16622K32HH55Z3Q082&Operation=ItemSearch&Keywords=DVD&SearchIndex=DVD&MinimumPrice=980&MaximumPrice=999&ItemPage=1","r")
or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);
mysql_close();
?>
feyd | Please use Code: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Sat Mar 11, 2006 9:11 am
by feyd
Posted: Sat Mar 11, 2006 9:19 am
by GeXus
I can't seem to use it, I always get a call to undifined funciton
Posted: Sat Mar 11, 2006 9:25 am
by feyd
sounds like you're running an older version than 4.3
try
mysql_escape_string() then. Sorry about being really terse before.. This would be called like
Code: Select all
$title = mysql_escape_string($title);
Posted: Sat Mar 11, 2006 9:30 am
by GeXus
feyd wrote:sounds like you're running an older version than 4.3
try
mysql_escape_string() then. Sorry about being really terse before.. This would be called like
Code: Select all
$title = mysql_escape_string($title);
That function worked! Thanks
But.... its still not inserting.. here is an example
This does NOT work
Code: Select all
$title = mysql_escape_string($title);
$query = "INSERT INTO dvds (ASIN, TITLE) VALUES ('$ASIN','$title')";
This DOES work
Code: Select all
$title = mysql_escape_string($title);
$query = "INSERT INTO dvds (ASIN, TITLE) VALUES ('$ASIN','Crash (Widescreen Edition)')";
And like I say, when I echo the title, it's working just fine... im at a loss
Posted: Sat Mar 11, 2006 9:32 am
by s.dot
are you using the correct case
IE: is it $Title instead of $title?
Posted: Sat Mar 11, 2006 9:33 am
by s.dot
and did you call mysql_query($query); ?
Posted: Sat Mar 11, 2006 9:33 am
by GeXus
scrotaye wrote:are you using the correct case
IE: is it $Title instead of $title?
Yeah, it should be all lower case, i just tried it the other way and had no luck either.. thanks!
Posted: Sat Mar 11, 2006 9:34 am
by GeXus
scrotaye wrote:and did you call mysql_query($query); ?
Yeah, the ASIN value is inserting just fine..
Posted: Sat Mar 11, 2006 9:35 am
by s.dot
i know you echoed it, but try echoing the entire query
Code: Select all
$title = mysql_escape_string($title);
echo "INSERT INTO dvds (ASIN, TITLE) VALUES ('$ASIN','$title')";
Posted: Sat Mar 11, 2006 9:39 am
by GeXus
scrotaye wrote:i know you echoed it, but try echoing the entire query
Code: Select all
$title = mysql_escape_string($title);
echo "INSERT INTO dvds (ASIN, TITLE) VALUES ('$ASIN','$title')";
Weired...
Code: Select all
INSERT INTO dvds (ASIN, TITLE) VALUES ('B00008DDXC','')INSERT INTO dvds (ASIN, TITLE) VALUES ('B00008DDXC','')INSERT INTO dvds (ASIN, TITLE) VALUES ('B00008DDXC','')INSERT INTO dvds (ASIN, TITLE) VALUES ('B00008DDXC','')INSERT INTO dvds (ASIN, TITLE) VALUES ('B00008DDXC','Harry Potter and the Chamber of Secrets (Widescreen Edition) (Harry Potter 2)')
It looks like it is looping through 5 times.. and the last one is good.. but thats not what shows when I echo just the title.. hmmm..
Posted: Sat Mar 11, 2006 9:52 am
by feyd
characterData() is being called five times. It might be good to move the insertion to endElement() but first making sure that $ASIN and $title are not blank.
Posted: Sat Mar 11, 2006 10:10 am
by GeXus
feyd wrote:characterData() is being called five times. It might be good to move the insertion to endElement() but first making sure that $ASIN and $title are not blank.
Works!!!!
Thank you!