jeeves245 Posted August 13, 2009 Share Posted August 13, 2009 (Sorry if this should be in the database forum) I've been testing my program out over the last few hours to make sure there's no errors, and unfortunately I found a big one that I can't explain. Hoping for a bit of help.. i'll explain the best I can. I have a database with the the fields psNumber (primary key) and estimatedDelivery (among others, but let's keep it simple). Values are put into the DB from a CSV file, then outputted back to the browser when the user requests it. When I go to upload the file, I get the error "Error: Duplicate entry '17273(or some other psNumber value)' for key 1" even though there are no duplicate values in the CSV file. But even with the error, all information still uploads correctly to the database. The real problem comes when outputting the values back to the browser. For some reason all rows are pulled out and put in the HTML table except 1. No idea why it's missing just 1 row. Hopefully someone can shed some light on this? This code opens the CSV file and puts everything into the database (obviously i'm not including ALL the code, just what should matter) $file_handle = fopen("./uploads/" . basename( $_FILES['uploadedfile']['name'])."", "rb"); while (!feof($file_handle) ) { $line_of_text = fgets($file_handle); $parts = explode(',', $line_of_text); $sql="INSERT INTO db (deliveryDate, psNumber, numItems, volume, estimatedDelivery, customerName, address1, address2, address3) VALUES ('$parts[0]','$parts[3]','$parts[4]','$parts[8]','$parts[1]','$parts[2]','$parts[5]','$parts[6]','$parts[7]')"; mysql_query($sql); And this is the code that brings it out: while($row = mysql_fetch_array($result)) { echo "<td>" . $row['estimatedDelivery'] . "</td>"; echo "<td>" . $row['psNumber'] . "</td>"; echo "<td>" . $row['customerName'] . "</td>"; echo "<td>" . $row['numItems'] . "</td>"; echo "<td>" . $row['volume'] . "</td>"; echo "<td>" . $row['address1'] . "</td>"; echo "<td>" . $row['address2'] . "</td>"; echo "<td>" . $row['address3'] . "</td>"; } And here is a sample line of the CSV file: 1/07/2009,16/07/2009,customername,170395,4,2,address1,address2,address3,0.46 So the basic problem is everything is output except for ONE random row... Any info appreciated. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/170070-solved-php-not-outputting-all-database-rows/ Share on other sites More sharing options...
ignace Posted August 13, 2009 Share Posted August 13, 2009 add an AUTO_INCREMENT to your primary key Quote Link to comment https://forums.phpfreaks.com/topic/170070-solved-php-not-outputting-all-database-rows/#findComment-897187 Share on other sites More sharing options...
jeeves245 Posted August 13, 2009 Author Share Posted August 13, 2009 add an AUTO_INCREMENT to your primary key Hm... The primary key is psNumber which already contains a unique value. Plus if I change it i'm going to have to rewrite code to suit. Quote Link to comment https://forums.phpfreaks.com/topic/170070-solved-php-not-outputting-all-database-rows/#findComment-897618 Share on other sites More sharing options...
jeeves245 Posted August 15, 2009 Author Share Posted August 15, 2009 Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/170070-solved-php-not-outputting-all-database-rows/#findComment-898616 Share on other sites More sharing options...
ignace Posted August 16, 2009 Share Posted August 16, 2009 add an AUTO_INCREMENT to your primary key Hm... The primary key is psNumber which already contains a unique value. Plus if I change it i'm going to have to rewrite code to suit. $sql="INSERT INTO db (deliveryDate, psNumber, numItems, volume, estimatedDelivery, customerName, address1, address2, address3) VALUES ('$parts[0]','$parts[3]','$parts[4]','$parts[8]','$parts[1]','$parts[2]','$parts[5]','$parts[6]','$parts[7]') ON DUPLICATE KEY psNumber = psNumber + 1"; Quote Link to comment https://forums.phpfreaks.com/topic/170070-solved-php-not-outputting-all-database-rows/#findComment-899336 Share on other sites More sharing options...
jeeves245 Posted August 16, 2009 Author Share Posted August 16, 2009 Thanks for that, all sorted Quote Link to comment https://forums.phpfreaks.com/topic/170070-solved-php-not-outputting-all-database-rows/#findComment-899361 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.