Problem inserting into database
Moderator: General Moderators
Problem inserting into database
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!
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!
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.
feyd | Please use
feyd | Please use
Code: Select all
andCode: 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
andCode: 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]I can't seem to use it, I always get a call to undifined funcitonfeyd wrote:try mysql_real_escape_string()
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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
try mysql_escape_string() then. Sorry about being really terse before.. This would be called like
Code: Select all
$title = mysql_escape_string($title);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 likeCode: 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')";Code: Select all
$title = mysql_escape_string($title);
$query = "INSERT INTO dvds (ASIN, TITLE) VALUES ('$ASIN','Crash (Widescreen Edition)')";are you using the correct case
IE: is it $Title instead of $title?
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.
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.
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.
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)')