lundi 20 avril 2015

MySQLi - inserting multiple rows into database

I have been asked to create a database for keeping track of roasting, packaging and ingredients used in the production of chocolate with data inserts through a web UI.

There are three types of ingredients used in the process of creating one batch. I want to add all three at once. There is only one table for all three.

I have the following table as part of a larger form:

<table>
    <tr class="mandatory">
        <td width="110">Batch No.</td>
        <td><input type="text" id="beanBatch" name="beanBatch" /></td>
    </tr>
    <tr class="mandatory">
         <td width="110">Weight</td>
         <td><input type="text" id="beanWeight" name="beanWeight" /></td>
    </tr>
    <tr>
         <td width="110">Roasted on</td>
         <td><input type="text" id="beanRoastDate" name="beanRoastDate" value="dd-mm-yyyy" /></td>
    </tr>
    <tr>
          <td width="110">Winnowed on</td>
          <td><input type="text" id="beanWinnowDate" name="beanWinnowDate" value="dd-mm-yyyy" /></td>
    </tr>
    <tr>
          <td width="110">Winnow Yield</td>
          <td><input type="text" id="beanWinnowYield" name="beanWinnowYield" /></td>
    </tr>
    <tr>
          <td width="110">Carry over</td>
          <td><input type="text" id="beanCarryOver" name="beanCarryOver" /></td>
    </tr>
</table>

<h3>Sugar</h3>
<table>
     <tr class="mandatory">
           <td width="110">Batch No.</td>
           <td><input type="text" id="sugarBatch" name="sugarBatch" /></td>
     </tr>
     <tr class="mandatory">
           <td width="110">Weight</td>
           <td><input type="text" id="sugarWeight" name="sugarWeight" /></td>
     </tr>
</table>

There's a third table for butter like the sugar table.

This is part of a POST-form. I have two SQL statements for packaging/products that work fine, so it isn't a connection issue.

I try to insert the ingredients like this:

$insert = $conn->query("INSERT INTO ingredients (batchNo, type, weight, roastDate, winnowDate, winnowYield, winnowCarryOver, batchCo) VALUES
($beanBatch, 'Beans', $beanWeight, $beanRoastDate, $beanWinnowDate, $beanWinnowYield, $beanCarryOver, $bCode),
($sugarBatch, 'Sugar', $sugarWeight, NULL, NULL, NULL, NULL, $bCode),
($butterBatch, 'Butter', $butterWeight, NULL, NULL, NULL, NULL, $bCode)");

if($insert){
    //return total inserted records using mysqli_affected_rows
    print 'Success! Total ' .$mysqli->affected_rows .' rows added.<br />'; 
}else{
    die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}

The error message is: Error : (1048) Column 'batchCo' cannot be null

However, I have given the bCode-field a value and therefore $bCode shouldn't be null... I think. I tried to print it before the database statements are executed but I can't see any output other than the error.

That's all there is, no further prepares or anything. I followed an example for this from a basic tutorial. Since there is no further information, I tried my luck with Google, where I got a lot of answers including foreach loops, but I don't think this is what's needed here.

I admittedly know very little about php/MySQL and my programming skills are... Basic. As in I know basic things in Java.

I'm sorry if this has been answered before.

Aucun commentaire:

Enregistrer un commentaire