PHP foreach INSERT into database with Datatables

I am trying to insert a line into a database for every row that has text within the text input in that row, I don’t have much experience using for-each loops so I’m probably doing something simple wrong…

This is my mark-up:

<form id="" action="test-price-quote.php" method="POST">
<table id="csp" class="table table-striped table-bordered display">
    <thead>
        <tr>
            <th>Product Code</th>
            <th>Product Name</th>
            <th>Band A Price</th>
            <th>Matrix</th>
            <th>New Price</th>
        </tr>
    </thead>
    <tbody>
        <?php while($res = sqlsrv_fetch_array($def, SQLSRV_FETCH_ASSOC)) : ?>
            <tr>                                
                <td><?php echo $res['ItemCode']; ?></td>
                    <input type="hidden" name="ItemCode[]" value="<?php echo $res['ItemCode']; ?>" />
                <td><?php echo $res['ItemName']; ?></td>
                    <input type="hidden" name="ItemName[]" value="<?php echo $res['ItemName']; ?>" />
                <td><?php echo $res['CurrentBandA']; ?></td>
                    <input type="hidden" name="CurrentBandA[]" value="<?php echo $res['CurrentBandA']; ?>" />
                <td><?php echo $res['Matrix']; ?></td>
                    <input type="hidden" name="Matrix[]" value="<?php echo $res['Matrix']; ?>" />
                <td><input type="text" name="csp-price[]" value=""></input></td>
            </tr>
        <?php endwhile; ?> 
    </tbody>
</table>
</form>

This is my PHP and Insert script:

<?php

    if (isset($_POST['submit'])) { 

        foreach ($_POST['csp-price'] as $price) { 
            $quoteID = $row['QuoteID'];

            $query = "INSERT INTO po_special_price_products (QuoteID, CardCode, SlpCode, SlpName, BandA, NewPrice) VALUES (?, ?, ?, ?, ?, ?)"; 
            $stmt = sqlsrv_prepare($sapconn2, $query); 

            if (sqlsrv_execute($stmt) === false) { 
                die(print_r(sqlsrv_errors(), true)); 
            } 
        } 
    }
?>

What I am expecting to happen is any row that has text inputted will insert that row of data into the database. I am also using jQuerys datatables, could this be affecting it not inserting?

I am currently getting “COUNT field incorrect or syntax error” from research my understanding that this is when I am not inserting the same amount of columns that are in the database, but I am, so maybe I have misunderstood this.

Any help in the right direction would be great, thanks in advance.


Source: stackoverflow-php

Click event on dynamic button in table

I have a datatables element on my page with a hidden first column, and an empty column at the end to contain a button.

I’m trying to get the click event of that button to:

  1. Hide the button,
  2. Show a ‘loading’ icon – fontawesome icon already in the column
  3. Retrieve the value of the hidden columns corresponding row
  4. Show a success/fail icon – to be added but will be a fontawesome icon

CSHTML:

@foreach (var item in Model.Items) {
DATA_ID KPI Name Value 1 Value 2 Value 3 Date For Value Type Added By Added On Action
@item.DATAID @item.KPIHead @item.Value1 @item.Value2 @item.Value3 @item.FromDate.ToString("dd/MM/yyyy") @item.Type @item.AddedBy @item.AddedOn.ToString("dd/MM/yyyy")
</td> </tr> } </tbody> </table> </div> </div>

Script:

$(document).ready(function () {
    var table = $('#SignOffTable').DataTable({
        paging: true,
        searching: false,
        ordering: false,
        pagingType: "simple_numbers",
        lengthChange: false,
        bfilter: true,
        info: true,
        "columnDefs": [
        { "visible": false, "targets": 0 }
        ]
    });
});

$("#SignOffTable button").click(function () {
    var trElem = $(this).closest("tr");
    var firstTd = $(trElem).children("td")[0];
    alert($(firstTd).text())
})

However I can't seem to access the hidden column's data, or successfully hide the button/show the loading spinner.


Source: stackoverflow-javascript