-1

I'm trying to make a program that keep track of some cargo containers and what is in them.

The form is divided in 2 parts. The first part ask about the shipment like if it is truck or container and expected arrival date. This info is stored in a DB lets call it shippinginfo. Upon adding it to the DB it get a unigue id (int auto inc). I also retrieve the ID to use to link the cargo to the shipping info.

The second part of the form is the cargo.. like article-lotno-qty instead of writing 20 rows i do a for loop The idea is to write the ID retrieved from the first part of the form (shipping info) and then the cargo info in another DB lets call it cargoDB

Here in my insert statement i use a for loop to loop trough the inserts to add them to the database but this is where it all goes to #¤%".

It adds the shipping info fine, i retrieve the id fine and it adds the first line of the cargo to the cargo DB but no matter what I do it will not add the other cargo lines to the DB.

I tried to print it to screen just to see the loop works and it prints just fine.

the code for the add to DB is

if($insert_stmt = $mysqli->prepare("INSERT INTO RegisterFPCargo (sID, artNo, lotNo, productionDate, bestbeforeDate, qty) VALUES (?, ?, ?, ?, ?, ?)")) {
    for($i = 1; $i <= $NumberofRows ; $i++){ 
        if($_POST['artNo'.$i] <> "") { 
            $insert_stmt->bind_param('issssi', $tID, $_POST['artNo'.$i], $_POST['lotNo'.$i], $_POST['pDate'.$i], $_POST['bbDate'.$i], $_POST['qty'.$i]);
            echo $_POST['artNo'.$i]."<br>"; //Debug
            $insert_stmt->execute();
        }
    }
}
echo $tID;

Please help me.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
hybris
  • 21
  • 5
  • `echo $_POST['artNo'.$i]."
    "; //Debug` that isn't the way to "debug". Get the real error(s) http://php.net/manual/en/mysqli.error.php --- http://php.net/manual/en/function.error-reporting.php
    – Funk Forty Niner Sep 25 '16 at 20:00
  • 1
    It was only to see if the for loop worked... It print all cargo rows to the screen alright but it only write the first cargoline to the db. Edit: Im on one.com webhosting and have error reporting on, but it doesnt state any errors. – hybris Sep 25 '16 at 20:37
  • Is there a primary key or unique index on the table? Can you run a `desc RegisterFPCargo;` query and show the results – tanerkay Sep 25 '16 at 20:41
  • Debug the mysqli_stmt with an `echo $insert_stmt->sqlstate;` or even dumping the full object, see if it helps. Maybe you can only bind once per prepared query ? – Proger_Cbsk Sep 25 '16 at 21:28
  • It would be rather useful to know how you set `$NumberofRows` Remember we **are not clairvoyant** – RiggsFolly Sep 25 '16 at 22:03
  • $Numberofrows=20; but @tanerkuc, yes i had a primary index on another column and when i removed that it worked :) Big Thanks :) – hybris Sep 26 '16 at 05:07

1 Answers1

0

As mentioned in comments, the issue was duplicate values on a column that was a primary key.

For future reference, the execute method returns false on failure and you can debug by inspecting the error message. The quick and dirty way would be to do:

$mysqli->execute($stmt) or trigger_error($mysqli->error);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
tanerkay
  • 3,819
  • 1
  • 19
  • 28