Jump to content

Only 4 of 7 rows inserting


thenorman138

Recommended Posts

I have a form, populated by an uploaded CSV, and upon submit it saves to my database staging/temp table. However, it's only saving the first 4 of  7 rows.
 
The form:
 
    $file = $_FILES["file"]["tmp_name"];
$handle = fopen($file, "r");
$maxPreviewRows = PHP_INT_MAX;  // this will be ~2 billion on 32-bit system, or ~9 quintillion on 64-bit system
$hasHeaderRow = true;
echo "<form method='post' action='/form-submit' >";
echo '<table>';
 
/*WE WILL NEED TO QA CONDITIONS AND HIGHLIGHT IN RED HERE. ALSO NEED BORDER STYLINGS*/
 
 
if ($hasHeaderRow) {
   $headerRow = fgetcsv($handle);
   echo '<thead><tr>';
   foreach($headerRow as $value) {
       echo "<th>$value</th>";
   }
   echo '</tr></thead>';
}
 
echo '<tbody>';
 
$rowCount = 0;
while ($row = fgetcsv($handle)) {
   $colCount = 0;
   echo '<tr>';
   foreach($row as $value) {
           echo "<td><input name='row[".$rowCount."][".$colCount."]' type='text' value='$value' /></td>";
 
                    $colCount++;
                    } 
   echo '</tr>';
 
   if (++$rowCount > $maxPreviewRows) {
       break;
   }
}
echo '</tbody></table>';
echo "<input type='submit' name='confirm' value='confirm'>";
 
 
                echo '</form>';
                }
 
The submission:
 
    foreach ($_POST['row'] as $rowValue)
    {
    if( is_array($rowValue) && count($rowValue) > 0 )
    {
        $sqlFull = "INSERT INTO staging VALUES( DEFAULT, ". substr( str_repeat ( '?, ', count($rowValue) ), 0, -2 ) .")";
        if( !$stmt = $connect->prepare($sqlFull) )
        {
            echo "Prepare failed: (" . $connect->errno . ") " . $connect->error;
        }
        $stmt->bind_param( str_repeat ( 's', count($rowValue) ), ...$rowValue );
 
        $stmt->execute();
    }
    }
 
So it's definitely working but for some reason it's stopping on the fourth row every time. I edit the first field of each row each time and when I check my file dashboard I'll see only the first 4 from each submission.
 
When I print_r($_POST) I get [0]array through [4]array, but only 4 rows actually submit to the database out of the 7 on the form.
 

 

ANy ideas why this is ending on the 4th row?
Link to comment
Share on other sites

Do you have any unique indexes set on the DB? It could also be a data problem. Copy the 4 rows that work and duplicate them in a file with only the 8 known good rows. Then see if all 8 rows get entered. If they do, then you have a data problem with the other rows.

 

Before you posted you wanted the csv data to be input to the DB. Now you're back to throwing a form in the middle of it. What exactly is it you want to do?

Edited by benanamen
Link to comment
Share on other sites

Ah good thinking, I did that and with the 4 good rows duplicated it works for all 8. I need to find out the issue on that 5th row, I guess.

 

I have it succesfully inserting to the database, but my project manager is wishy-washy on seeing an editable form work. He was fine with editing from the database but then he wanted to address him again. I worked on an easier loop, above, and got it working but I just started having this issue with only the first 4 rows going in.

Link to comment
Share on other sites

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.