Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Why are the wrong values getting into my database?

Posted on 2011-09-22
2
Medium Priority
?
241 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 2000 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 84

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …
Suggested Courses

963 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