Jump to content

[SOLVED] PHP not outputting all database rows


jeeves245

Recommended Posts

(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.

 

 

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";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.