Solved

Why are the wrong values getting into my database?

Posted on 2011-09-22
2
235 Views
Last Modified: 2012-05-12
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

picture of the wrong values in my database
0
Comment
Question by:NewWebDesigner
2 Comments
 
LVL 4

Accepted Solution

by:
sakman earned 500 total points
ID: 36584160
The maximum size for MySQL tinyint (in your Merchant_id column) is 127 (signed) or 255 (unsigned).

http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 36584164
'$merchant->merchant_id' should not be quoted in your SQL statement since you have defined it as a 'tinyint'.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now