Jump to content

Recommended Posts

I'm having a problem adding records to my database.

 

I have a file called products.txt, here are a few of the records so you guys know what im working with.

 

453786877|Splish Splash Duckie Pajamas for Girls, Toddlers, and Infants|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=453786877|http://dts.ystoretools.com/1002/images/100x500/spspdupaforg.jpg|http://dts.ystoretools.com/1002/images/250x1000/spspdupaforg.jpg|12.99||Fashion|Girls|These adorable girl's pajamas feature a rubber duck print. They have short sleeves and button up the front of the top. The fabric is 50% cotton and 50% polyester. They should be worn snug fitting. These are excellent quality pajamas.  These pajamas run small. We recommend purchasing one size larger than normal.|Clearance Section||24.00|||2007-03-26 11:45:02.320|instock|CrazyforBargains: Fun Family Sleepwear||Clearance Section||||
453786879|Rudolph the Red Nosed Reindeer Pajamas for Kids, Toddlers, and Infants|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=453786879|http://dts.ystoretools.com/1002/images/100x500/rurednorepaf.jpg|http://dts.ystoretools.com/1002/images/250x1000/rurednorepaf.jpg|24.00||Fashion|Girls|These adorable kid's pajamas feature a Rudolph the Red Nosed Reindeer print. They have long sleeves. The fabric is 50% cotton and 50% polyester. They should be worn snug fitting. These are excellent quality pajamas.|Girl's Sleepwear|Christmas Pajamas for Girls|24.00|||2007-03-09 23:38:47.140|instock|CrazyforBargains: Fun Family Sleepwear||Girl's Sleepwear|Christmas Pajamas for Girls|||
453786882|Lone Ranger Cotton Set for Boys, Toddlers, and Infants|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=453786882|http://dts.ystoretools.com/1002/images/100x500/loracosetfor.gif|http://dts.ystoretools.com/1002/images/250x1000/loracosetfor.gif|21.99||Fashion|Boys|This adorable two-piece set features a Lone Ranger print. It comes complete with a coordinating mask. It has short sleeves and long pants. The fabric is 100% cotton and should be worn snug fitting. The mask should not be worn while the child is sleeping.|Clearance Section||28.00|||2007-03-26 11:45:02.320|instock|CrazyforBargains: Fun Family Sleepwear||Clearance Section||||
454063058|Fun Boxers Feliz Navidad Christmas Boxer Shorts for Men|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=454063058|http://dts.ystoretools.com/1002/images/100x500/funbofenachb.gif|http://dts.ystoretools.com/1002/images/250x1000/funbofenachb.gif|9.99||Fashion|Mens|These men's Christmas boxer shorts are Fun Boxers brand. The print features the words Feliz Navidad. The fabric is 100% cotton knit. The waistband is elastic.|Men's Boxer Shorts|Christmas Boxer Shorts|9.99|||2007-03-09 23:38:47.140|instock|CrazyforBargains: Fun Family Sleepwear||Men's Boxer Shorts|Christmas Boxer Shorts|||
454063059|Fun Boxers Have a Cool Yule Christmas Boxer Shorts for Men|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=454063059|http://dts.ystoretools.com/1002/images/100x500/funbohacoyuc.gif|http://dts.ystoretools.com/1002/images/250x1000/funbohacoyuc.gif|9.99||Fashion|Mens|These men's Christmas boxer shorts are Fun Boxers brand. The print features Santas in sunglasses. The fabric is 100% cotton knit. The waistband is elastic.|Men's Boxer Shorts|Christmas Boxer Shorts|9.99|||2007-03-09 23:38:47.140|instock|CrazyforBargains: Fun Family Sleepwear||Men's Boxer Shorts|Christmas Boxer Shorts|||
454131196|Animal Cotton Toddler Pajamas for Boys|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=454131196|http://dts.ystoretools.com/1002/images/100x500/ancotopaforb.gif|http://dts.ystoretools.com/1002/images/250x1000/ancotopaforb.gif|13.99||Fashion|Boys|These adorable 100% cotton pajamas are white and feature an animal print. They are meant to be snug fitting. These are excellent quality pajamas.|Toddler Boy Pajamas & Robes|Long Sleeved Cotton Toddler Pajamas|13.99|||2007-03-09 23:38:47.140|instock|CrazyforBargains: Fun Family Sleepwear||Toddler Boy Pajamas & Robes|Long Sleeved Cotton Toddler Pajamas|||
454131197|Ballerina Cotton Toddler Pajamas for Girls|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=454131197|http://dts.ystoretools.com/1002/images/100x500/bacotopaforg.gif|http://dts.ystoretools.com/1002/images/250x1000/bacotopaforg.gif|13.99||Fashion|Girls|These adorable 100% cotton pajamas are white and feature a ballerina print. They are meant to be snug fitting. These are excellent quality pajamas.|Toddler Pajamas & Robes for Girls|Long Sleeved Cotton Pajamas|13.99|||2007-03-09 23:38:47.140|instock|CrazyforBargains: Fun Family Sleepwear||Toddler Pajamas & Robes for Girls|Long Sleeved Cotton Pajamas|||
454131202|Fish Slippers for Men and Women|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=454131202|http://dts.ystoretools.com/1002/images/100x500/fislformenan.gif|http://dts.ystoretools.com/1002/images/250x1000/fislformenan.gif|18.99||Fashion|Boys|These adorable colorful fish slippers fit both women and men. These slippers cover the entire foot. The sole is made of one inch thick foam. They are a very fine quality slipper. Sizing: Large fits a women's size 10+ or a men's shoe size 9-11.|Men's Pajamas|Shop by Theme or Character Pajamas for Men|18.99|||2007-04-21 11:45:09.733|instock|CrazyforBargains: Fun Family Sleepwear||Men's Pajamas|Shop by Theme or Character Pajamas for Men|||
454255502|Rocket Cotton Pajamas for Toddler and Boys|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=454255502|http://dts.ystoretools.com/1002/images/100x500/rocotopaforb.gif|http://dts.ystoretools.com/1002/images/250x1000/rocotopaforb.gif|13.99||Fashion|Boys|These adorable 100% cotton pajamas are blue and feature a rocket print. They are meant to be snug fitting. These are excellent quality pajamas.|Boy's Sleepwear & Pajamas|Long Sleeved Cotton Pajamas|13.99|||2007-03-09 23:38:47.140|instock|CrazyforBargains: Fun Family Sleepwear||Boy's Sleepwear & Pajamas|Long Sleeved Cotton Pajamas|||
454255505|Sports Cotton Pajamas for Toddler and Boys|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=454255505|http://dts.ystoretools.com/1002/images/100x500/spcotopaforb.gif|http://dts.ystoretools.com/1002/images/250x1000/spcotopaforb.gif|13.99||Fashion|Boys|These adorable 100% cotton pajamas are green and feature a sports print. They are meant to be snug fitting. These are excellent quality pajamas.|Boy's Sleepwear & Pajamas|Long Sleeved Cotton Pajamas|13.99|||2007-03-09 23:38:47.140|instock|CrazyforBargains: Fun Family Sleepwear||Boy's Sleepwear & Pajamas|Long Sleeved Cotton Pajamas|||
454255508|Turtle Slippers for Men and Women|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=454255508|http://dts.ystoretools.com/1002/images/100x500/tuslformenan.gif|http://dts.ystoretools.com/1002/images/250x1000/tuslformenan.gif|18.99||Fashion|Boys|These adorable turtle slippers fit both women and men. These slippers cover the entire foot. The sole is made of one inch thick foam. They are a very fine quality slipper. Sizing: Medium fits a women's shoe size 7-9 1/2.|Men's Pajamas|Gifts for Men: Bath Robes, Shower Wraps, Pajamas and Slippers|18.99|||2007-03-09 23:38:47.140|instock|CrazyforBargains: Fun Family Sleepwear||Men's Pajamas|Gifts for Men: Bath Robes, Shower Wraps, Pajamas and Slippers|||
454265555|Black Bear Paw Slippers for Men and Women|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=454265555|http://dts.ystoretools.com/1002/images/100x500/blbepawslfor.gif|http://dts.ystoretools.com/1002/images/250x1000/blbepawslfor.gif|18.99||Fashion|Womens|These adorable black bear paw slippers fit women and men. These slippers cover the entire foot. The sole is made of one inch thick foam. They are a very fine quality slipper. Sizing: Medium fits a women's shoe size 7-9 1/2. Large fits a women's size 10+ or a men's shoe size 9-11. These slippers were seen on Good Morning America!|Our Most Popular Sleepwear||18.99|||2007-03-09 23:38:47.140|instock|CrazyforBargains: Fun Family Sleepwear||Our Most Popular Sleepwear||||
454265563|Elephant Slippers for Kids, Men and Women|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=454265563|http://dts.ystoretools.com/1002/images/100x500/elslformenan.gif|http://dts.ystoretools.com/1002/images/250x1000/elslformenan.gif|18.99||Fashion|Womens|These adorable elephant slippers fit both women and men. These slippers cover the entire foot. The sole is made of one inch thick foam. They are a very fine quality slipper. Sizing: Small fits a child age 6-9 or a women's size 4-6 1/2. Medium fits a women's shoe size 7-9 1/2. Large fits a women's size 10+ or a men's shoe size 9-11.|Men's Pajamas|Gifts for Men: Bath Robes, Shower Wraps, Pajamas and Slippers|18.99|||2007-03-09 23:38:47.140|instock|CrazyforBargains: Fun Family Sleepwear||Men's Pajamas|Gifts for Men: Bath Robes, Shower Wraps, Pajamas and Slippers|||
454265572|Hot Air Balloon Pajamas for Toddlers and Girls|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=454265572|http://dts.ystoretools.com/1002/images/100x500/hotairbatopa.gif|http://dts.ystoretools.com/1002/images/250x1000/hotairbatopa.gif|12.99||Fashion|Girls|These adorable coat-style pajamas feature a hot air balloon print. The fabric is 100% polyester and flame resistant. These pajamas were made for a major upscale department store.|Clearance Section||16.99|||2007-03-26 11:45:02.320|instock|CrazyforBargains: Fun Family Sleepwear||Clearance Section||||
454265577|Pig Slippers for Men and Women|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=454265577|http://dts.ystoretools.com/1002/images/100x500/pigslformena.gif|http://dts.ystoretools.com/1002/images/250x1000/pigslformena.gif|18.99||Fashion|Womens|These adorable pig slippers fit both women and men. These slippers cover the entire foot. The sole is made of one inch thick foam. They are a very fine quality slipper. Sizing: Medium fits a women's shoe size 7-9 1/2. Large fits a women's size 10+ or a men's shoe size 9-11.|Men's Pajamas|Gifts for Men: Bath Robes, Shower Wraps, Pajamas and Slippers|18.99|||2007-03-09 23:38:47.140|instock|CrazyforBargains: Fun Family Sleepwear||Men's Pajamas|Gifts for Men: Bath Robes, Shower Wraps, Pajamas and Slippers|||
454265581|Pink Cotton Floral Pajamas for Girls|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=454265581|http://dts.ystoretools.com/1002/images/100x500/picoflpaforg.gif|http://dts.ystoretools.com/1002/images/250x1000/picoflpaforg.gif|13.99||Fashion|Girls|These adorable 100% cotton pajamas are pink and feature a floral print. They are meant to be snug fitting. These are excellent quality pajamas.|Girl's Sleepwear|Long Sleeved Cotton Pajamas|13.99|||2007-03-21 12:47:59.550|instock|CrazyforBargains: Fun Family Sleepwear||Girl's Sleepwear|Long Sleeved Cotton Pajamas|||
454265582|Pink Flamingo Slippers for Men, Women and Kids|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=454265582|http://dts.ystoretools.com/1002/images/100x500/piflslformen.gif|http://dts.ystoretools.com/1002/images/250x1000/piflslformen.gif|18.99||Fashion|Womens|These adorable pink flamingo slippers fit both women and men. These slippers cover the entire foot. The sole is made of one inch thick foam. They are a very fine quality slipper. Sizing: Small fits children 6 to 9 years old. Medium fits a women's shoe size 7-9 1/2. Large fits a women's size 10+ or a men's shoe size 9-11.|Men's Pajamas|Gifts for Men: Bath Robes, Shower Wraps, Pajamas and Slippers|18.99|||2007-03-09 23:38:47.140|instock|CrazyforBargains: Fun Family Sleepwear||Men's Pajamas|Gifts for Men: Bath Robes, Shower Wraps, Pajamas and Slippers|||
454265584|Purple Animal Print Panne Pajamas for Girls|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=454265584|http://dts.ystoretools.com/1002/images/100x500/puanprpapafo.gif|http://dts.ystoretools.com/1002/images/250x1000/puanprpapafo.gif|11.99||Fashion|Girls|These adorable coat-style pajamas feature a lavender animal print. The fabric is 100% polyester panne and flame resistant. These pajamas were made for a major upscale department store.|Girl's Sleepwear|Novelty Print Pajamas|16.99|||2007-03-26 11:45:02.320|instock|CrazyforBargains: Fun Family Sleepwear||Girl's Sleepwear|Novelty Print Pajamas|||
454367516|Keepin' It Reel Fishing Boxer Shorts Gift Set|5181|CrazyforBargains.com|http://www.shareasale.com/m-pr.cfm?merchantID=5181&userID=211655&productID=454367516|http://dts.ystoretools.com/1002/images/100x500/keitrefibosh.gif|http://dts.ystoretools.com/1002/images/250x1000/keitrefibosh.gif|8.99||Fashion|Mens|This adorable gift set includes one pair of cotton knit "Keepin' It Reel" fishing boxer shorts and a fish pen complete in a gift box.|Men's Pajamas|Shop by Theme or Character Pajamas for Men|16.99|||2007-04-21 11:45:09.733|instock|CrazyforBargains: Fun Family Sleepwear||Men's Pajamas|Shop by Theme or Character Pajamas for Men|||

 

As you can see its a datafeed with a list of all the products this company offers.

 

What i attempted to do was write a script that broke the file down, put each records info into an array and then load it into a database.

 

It seems to work ok, except for the fact that it only loads 348 of the 1367 records in the products.txt file. The wierd thing is that the records that make it into the database seem to be random. Its not just the first 348 that are getting entered.

 

Anyway, here is the code, maybe you guys can help shed some light on whats going on, because im confused.

 

 

 

 

<?php
// File name:   feedinject.php
// Author:      Tyler Biscoe
// Date:        05 May 2007 
// 
// Description:
// This file loads the crazyforbargains.com datafeed, 
// and inserts the data into a database.
//



// Connect to the database and select database
echo "Connecting to Database...";
include("include/connect.php");

// Load file and add it to the array $lines
echo "Loading File...";
$lines = file('http://www.tylerbiscoe.com/phptest/datafeeds/products.txt');
echo "Done. <br /> <br />";

// Insert the data into the database
echo "Inserting Data...";

$lines = file('http://www.tylerbiscoe.com/phptest/datafeeds/products.txt');


// Loop through the results and add each items information into the database.
foreach ($lines as $line_num => $line) {

$element[$line_num] = explode("|", $lines[$line_num]);


$vendor = $element[$line_num][3];
$itemName = $element[$line_num][1];
$itemNumber = $element[$line_num][0];
$catergory = $element[$line_num][21];
$spicture_url = $element[$line_num][5];
$lpicture_url = $element[$line_num][6];
$description = $element[$line_num][11];
$price = $element[$line_num][14];
$status = $element[$line_num][18];
$link = $element[$line_num][4];


$query = mysql_query("INSERT INTO items (id, name , catergory, spicture_url, lpicture_url, description, price, status, vendor, url) VALUES ('$itemNumber','$itemName','$catergory','$spicture_url','$lpicture_url','$description','$price','$status','$vendor','$link')");

}

echo "Done. <br /><br />";



echo "Process completed.";


?>
    

 

Thanks alot!

 

Link to comment
https://forums.phpfreaks.com/topic/50182-solved-problem-adding-records/
Share on other sites

I do not understand these two seperate calls to the file() function.

 

<?php

// Load file and add it to the array $lines
echo "Loading File...";
$lines = file('http://www.tylerbiscoe.com/phptest/datafeeds/products.txt');
echo "Done. <br /> <br />";

// Insert the data into the database
echo "Inserting Data...";

$lines = file('http://www.tylerbiscoe.com/phptest/datafeeds/products.txt');

?>

 

Place a count straight after the above code to see how many lines are returned:

<?php

exit("<PRE>\n\nThe number of lines: ".sizeof(lines)."\n\n</PRE>");

?>

 

consider changing this:

 

<?php

foreach ($lines as $line_num => $line) {

$element[$line_num] = explode("|", $lines[$line_num]);


$vendor = $element[$line_num][3];
$itemName = $element[$line_num][1];
$itemNumber = $element[$line_num][0];
$catergory = $element[$line_num][21];
$spicture_url = $element[$line_num][5];
$lpicture_url = $element[$line_num][6];
$description = $element[$line_num][11];
$price = $element[$line_num][14];
$status = $element[$line_num][18];
$link = $element[$line_num][4];

?>

 

to this:

 

<?php

foreach ($lines as $line) {

$element = explode("|", $line);


$vendor = $element[3];
$itemName = $element[1];
$itemNumber = $element[0];
$catergory = $element[21];
$spicture_url = $element[5];
$lpicture_url = $element[6];
$description = $element[11];
$price = $element[14];
$status = $element[18];
$link = $element[4];

?>

 

and finally - print out each insert to the screen to be sure that each insert is there and not failing at the mysql level.

Ok i added:

 

exit("<PRE>\n\nThe number of lines: ".sizeof(lines)."\n\n</PRE>");

 

And it returned: 1

 

Then i switched the loop i was using to the one you suggested.

 

And then i added a script to show which records were being added and which were not.

 

The records in green were successfully added, while the ones in red were not.

 

Please go see for yourself: http://www.tylerbiscoe.com/phptest/feedinject.php

 

Oh and as you can see from the code, i added TRUNCATE TABLE to the end so i wouldnt have to keep deleting the records and starting over.... That will obviously be removed later.

 

 

Here is the code i am now using.

 

<?php
// File name:   crzyfeedinject.php
// Author:      Tyler Biscoe
// Date:        05 May 2007 
// 
// Description:
// This file loads the crazyforbargains.com datafeed, 
// and inserts the data into a database.
//



// Connect to the database and select database
echo "Connecting to Database...";
include("include/connect.php");

// Load file and add it to the array $lines
echo "Loading File...";
$lines = file('http://www.tylerbiscoe.com/phptest/datafeeds/products.txt');
echo "Done. <br /> <br />";

// Insert the data into the database
echo "Inserting Data... <br />";




// Loop through the results and add each items information into the database.
foreach ($lines as $line) {

$element = explode("|", $line);


$vendor = $element[3];
$itemName = $element[1];
$itemNumber = $element[0];
$catergory = $element[21];
$spicture_url = $element[5];
$lpicture_url = $element[6];
$description = $element[11];
$price = $element[14];
$status = $element[18];
$link = $element[4];


$query = mysql_query("INSERT INTO items (id, name , catergory, spicture_url, lpicture_url, description, price, status, vendor, url) VALUES ('$itemNumber','$itemName','$catergory','$spicture_url','$lpicture_url','$description','$price','$status','$vendor','$link')");

if($query) {

echo "<font color=#009900>".$line."</font><br /><br />";

} else { 

echo "<font color=#FF0000>".$line."</font><br /><br />";

}









}

echo "Done. <br /><br />";



echo "Process completed.";


mysql_query("TRUNCATE TABLE items"); 

?>
    

 

Oh and i also got rid of one of the

$lines = file('http://www.tylerbiscoe.com/phptest/datafeeds/products.txt');

 

 

I cant figure it out.

 

Thanks for the help

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.