简体   繁体   中英

how to copy the selected row in datatable to another table in database mysql

i have data in products in database and it all display to my datatable in php file. and now i have the selected rows in datatable and i want all the selected rows transfer to another table in database like from products to sales_order_holder when clicking the submit button. is it possible? .

<table id="example" cellspacing="0" width="100%" class="display">
  <thead>
    <tr>
      <th></th>
      <th>Category</th>
      <th>Brand</th>
      <th>Model</th>
      <th>Item Name</th>
      <th>Price</th>
      <th>id</th>
    </tr>
  </thead>
  <tbody>
    <?php do { ?>
    <tr>
      <td><?php echo ++$i; ?></td>
      <td><?php echo $row_products['pid']; ?></td>
      <td><?php echo $row_products['brand']; ?></td>
      <td><?php echo $row_products['model']; ?></td>
      <td><?php echo $row_products['item_name_']; ?></td>
      <td><?php echo $row_products['price']; ?></td>
      <td><?php echo $row_products['id']; ?></td>
    </tr>
    <?php } while ($row_products = mysql_fetch_assoc($products)); ?>
  </tbody>
</table>

Use this,

INSERT INTO table2 (col1,col2...)
SELECT * FROM table1 WHERE selected row's id

If more than 1 row is selected, try using foreach or use INSERT BATCH to insert and WHERE IN in select

INSERT INTO table2 (val1, val2)
SELECT val1,val2 FROM table1 WHERE tableRowId = someID

You can have a check box beside each row in the displayed table.These checkboxes should contain the id of the product as value. Then pass on the values of these checkboxes using ajax to some php script which will run the query.

$(document).ready(function() {
$("#submit").click(function() {
  var x=0;
   console.log("start");

    var selected = [];
    $("input:checkbox[id=product]:checked").each(function(){
    selected.push($(this).val());
    });
    if (typeof selected !== 'undefined' && selected.length > 0) {

      var target= $("#target").val() ;
      var data = {
        products : selected.toString(),

    }



    $.ajax({
     url:"/dispatch",
     type: "POST",
     data:JSON.stringify(data),
     dataType: "json",
     contentType: "application/json",
     success: function(data)
     {           
            console.log("end");

     }
});

      }

});
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM