Link to home
Start Free TrialLog in
Avatar of NewWebDesigner
NewWebDesigner

asked on

Why are the wrong values getting into my database?

Hello,

For the life of me I can't figure out why the wrong data is getting into my database.  My code is trying to read certain values from an XML page into a database.  I am trying to grab the merchant and the merchant ID from the XML document.  When I echo the variables representing the merchant and merchant id, The merchant id matches up with the appropriate merchant.  But When I check my database, somehow some of these merchant ids don't match up with the appropriate merchant.  For reasons I don't understand,  the merchant id "127" appears for multiple values even when that merchant id doesn't exist for any merchant.  Attached is my php and my sql code.

To recap:  Below are the advertisers and their correct ids.  When you run my code and look at the database, you will see that some of the merchant-ids are "127" which wrong and doesn't match up with the XML.  Why are the correct merchant-ids not getting put into the database?


Amazon.com
3
BarnesAndNoble.com
4
BiggerBooks.com
43
BookRenter
314
CampusBookRentals
305
CengageBrain.com
49
College Book Renter
310
CourseSmart
64
eCampus Marketplace
63
eCampus Rental
317
eCampus.com
17
PhatCampus.com
33
PowellsBooks.com
11
TextbookStop.com
50


Any help is appreciated,

Thanks,

<?php
$query = 'http://www.webdesignrandd.com/merchantcoupons.xml';
$tmp_obj_xml_coupon = simplexml_load_file($query);
$i=0;
//$con = mysqli_connect("localhost","root","");

//mysql_select_db("pricecomp",$con);
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("pricecomp", $con);






foreach($tmp_obj_xml_coupon->page->merchant as $merchant) 
{
	echo $name = $merchant->name . '<br />';
	echo $id = $merchant->merchant_id . '<br />';
	
	
	mysql_query("INSERT INTO coupons VALUES (NULL, '$merchant->name', 'blah', '$merchant->merchant_id', 'tools')");
	$sellers[] = $merchant->name;
       while($i<count($sellers)) 
	   {
       foreach($tmp_obj_xml_coupon->page->merchant[$i++]->coupons->coupon as $coupon)
          {
                               
								//echo $coupon->desc;
                                echo "</br>";
		  }  
	   }
}
mysql_close($con);


print_r($tmp_obj_xml_coupon);

?>

name
image
merchant_id
coupon
 mysql_query("INSERT INTO coupons (Name, Image, Merchant_Id, Coupon)
VALUES ('Amazon', 'www.hotornot.com', '34', '10 dollars off')");

Open in new window


CREATE TABLE IF NOT EXISTS `coupons` (
  `Primary` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(50) NOT NULL,
  `Image` varchar(200) NOT NULL,
  `Merchant_id` tinyint(4) NOT NULL,
  `Coupon` varchar(1000) NOT NULL,
  PRIMARY KEY (`Primary`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Open in new window

User generated image
ASKER CERTIFIED SOLUTION
Avatar of sakman
sakman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dave Baldwin
'$merchant->merchant_id' should not be quoted in your SQL statement since you have defined it as a 'tinyint'.