Solved

Why are the wrong values getting into my database?

Posted on 2011-09-22
2
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

751 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