Solved

Why are the wrong values getting into my database?

Posted on 2011-09-22
2
234 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 82

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
The viewer will learn how to count occurrences of each item in an array.

760 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

25 Experts available now in Live!

Get 1:1 Help Now