Problem inserting into database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Problem inserting into database

Post 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!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

could you please post your code for us to look at
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

feyd | Please use

Code: Select all

and

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

and

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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

I can't seem to use it, I always get a call to undifined funciton
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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);
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

are you using the correct case

IE: is it $Title instead of $title?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

and did you call mysql_query($query); ?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post 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!
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

scrotaye wrote:and did you call mysql_query($query); ?
Yeah, the ASIN value is inserting just fine..
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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')";
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post 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..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post 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!
Post Reply