Php 如何将html重复字段值更新到mysql数据库
我有一个html数组中的重复值,我需要更新到数据库中,我需要一个关于如何使用php脚本更新到staffid mataches所在的数据库的帮助。请参见下面我的HTML代码Php 如何将html重复字段值更新到mysql数据库,php,html,mysql,ajax,Php,Html,Mysql,Ajax,我有一个html数组中的重复值,我需要更新到数据库中,我需要一个关于如何使用php脚本更新到staffid mataches所在的数据库的帮助。请参见下面我的HTML代码 薪金计算样本 员工身份证 薪水 总税 净工资总额 我建议重命名输入名称,以创建一个包含所有所需信息的数组。将staff ID用作数组键 <!--Staff 1--> <tr data-id="STAFF/2016/001"> <td> <input type
薪金计算样本
员工身份证
薪水
总税
净工资总额
我建议重命名输入名称,以创建一个包含所有所需信息的数组。将staff ID用作数组键
<!--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>
在PHP中,您可以迭代此单个数组:
<?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 WHERE staffid = :staffid');
foreach ((array)$_POST['staff'] as $staffId => $staffInfo) {
$stmt->bindValue(':salary', $staffInfo['salary']);
$stmt->bindValue(':tax', $staffInfo['tax']);
$stmt->bindValue(':total', $staffInfo['total']);
$stmt->bindValue(':staffid', $staffId); // you can also use $staffInfo['id'] here instead of $staffId
$stmt->execute();
}
}
我建议重命名输入名称,以创建一个包含所有所需信息的数组。将staff ID用作数组键
<!--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>
在PHP中,您可以迭代此单个数组:
<?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 WHERE staffid = :staffid');
foreach ((array)$_POST['staff'] as $staffId => $staffInfo) {
$stmt->bindValue(':salary', $staffInfo['salary']);
$stmt->bindValue(':tax', $staffInfo['tax']);
$stmt->bindValue(':total', $staffInfo['total']);
$stmt->bindValue(':staffid', $staffId); // you can also use $staffInfo['id'] here instead of $staffId
$stmt->execute();
}
}
“我希望SQL更新查询看起来像”希望您不会。因为您将HTTP输入变量直接抛出到SQL字符串中,而不进行任何转义。请使用准备好的语句使用用户输入查询数据库。“我希望SQL更新查询看起来像”希望您不会。因为您将HTTP输入变量直接抛出到SQL字符串中,而不进行任何转义。请使用准备好的语句使用用户输入查询数据库。非常感谢。根据你上面的建议,我能把它做好。这真的很有帮助,非常感谢。根据你上面的建议,我能把它做好。这真的很有帮助