php - How to update html repeated field values to mysql database -
i have html repeated values in array need update database, need on how use php script update database staffid mataches . see below html code
<!doctype html> <html> <head lang="en"> <meta chartaxt="utf-8"> <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet"> <script src="js/jquery.min.js"></script> <script src="bootstrap/js/bootstrap.min.js"></script> <title>sample salary computation</title> </head> <body> <form method="post" name="regis" id="regis"> <table align="center" border="1" width="200"> <tr> <th>staff id</th> <th>salary</th> <th>total tax</th> <th>total net pay</th> </tr> <!--staff 1--> <tr data-id="staff/2016/001"> <td> <input type="text" name="staffid[]" class="staffid" value="staff/2016/001" readonly="readonly"> </td> <td> <input type="text" name="salary[]" class="salary" placeholder="salary" value="5000"> </td> <td> <input type="text" name="tax[]" class="tax" placeholder="tax" value="500"> </td> <td> <input type="text" name="total[]" class="total" placeholder="total" readonly="readonly" value="4500"> </td> </tr> <!--staff 2--> <tr data-id="staff/2016/002"> <td> <input type="text" name="staffid[]" class="staffid" value="staff/2016/002" readonly="readonly"> </td> <td> <input name="salary[]" class="salary" type="text" placeholder="salary" value="10000"> </td> <td> <input type="text" placeholder="tax" name="tax[]" class="tax" value="1000"> </td> <td> <input type="text" placeholder="total" class="total" name="total[]" readonly="readonly" value="9000"> </td> </tr> <!--staff 3--> <tr data-id="staff/2016/003"> <td> <input type="text" name="staffid[]" class="staffid" value="staff/2016/003" readonly="readonly"> </td> <td> <input name="salary[]" class="salary" type="text" placeholder="salary" value="8400"> </td> <td> <input type="text" placeholder="tax" name="tax[]" class="tax" value="400"> </td> <td> <input type="text" placeholder="total" class="total" name="total[]" readonly="readonly" value="800"> </td> </tr> <tr> <td width="300"> <input type="button" name="update" value="update values" class="btn btn-danger"> </td> </tr> </table> </form> </body> </html>
kindly assist. want sql update query
"update payroll set salary ='".$_post['salary']."', tax='".$_post['tax']."', total ='".$_post['total']."' staffid= '".$_post['staffid']."'"
i recommend rename input names create single array information need. use staff id array keys.
<!--staff 1--> <tr data-id="staff/2016/001"> <td> <input type="text" name="staff[staff/2016/001][id]" class="staffid" value="staff/2016/001" readonly="readonly"> </td> <td> <input type="text" name="staff[staff/2016/001][salary]" class="salary" placeholder="salary" value="5000"> </td> <td> <input type="text" name="staff[staff/2016/001][tax]" class="tax" placeholder="tax" value="500"> </td> <td> <input type="text" name="staff[staff/2016/001][total]" class="total" placeholder="total" readonly="readonly" value="4500"> </td> </tr> <!--staff 2--> <tr data-id="staff/2016/002"> <td> <input type="text" name="staff[staff/2016/002][id]" class="staffid" value="staff/2016/002" readonly="readonly"> </td> <td> <input type="text" name="staff[staff/2016/002][salary]" class="salary" placeholder="salary" value="10000"> </td> <td> <input type="text" name="staff[staff/2016/002][tax]" placeholder="tax" class="tax" value="1000"> </td> <td> <input type="text" name="staff[staff/2016/002][total]" placeholder="total" class="total" readonly="readonly" value="9000"> </td> </tr>
in php can iterate on single array:
<?php $db = new pdo('mysql:host=localhost;dbname=database;', 'root', ''); if (!empty($_post['staff'])) { $stmt = $db->prepare('update payroll set salary = :salary, tax = :tax, total = :total staffid = :staffid'); foreach ((array)$_post['staff'] $staffid => $staffinfo) { $stmt->bindvalue(':salary', $staffinfo['salary']); $stmt->bindvalue(':tax', $staffinfo['tax']); $stmt->bindvalue(':total', $staffinfo['total']); $stmt->bindvalue(':staffid', $staffid); // can use $staffinfo['id'] here instead of $staffid $stmt->execute(); } }
see also:
Comments
Post a Comment