dps69208 2012-12-04 19:22
浏览 14

更新多行php mysql

im trying to update multiple rows in the following code http://pastie.org/5480319. the problem is that i have more than 1 id_projectemployee in the WHERE clause so i need to match each value in the correct member. Up to now my code is only updating the last row and is setting the same value for id_employee as you can see in the following print_r

Array
(
[nom] => Backbone 34
[projectDate] => 2012-11-13
[province] => 9
[client] => Anapo SA
[id_client] => 16
[projectmanager] => Lautaros
[description] => ssss
[status] => 1
[idstaff] => Array
    (
        [0] => 9
        [1] => 10
    )

[tableEmployee] => Martin
[id_employee] => Array
    (
        [0] => 79
        [1] => 79
    )

[grabar] => si
[id] => 12
)
Array
(
[nom] => Backbone 34
[projectDate] => 2012-11-13
[province] => 9
[client] => Anapo SA
[id_client] => 16
[projectmanager] => Lautaros
[description] => ssss
[status] => 1
[idstaff] => Array
    (
        [0] => 9
        [1] => 10
    )

[tableEmployee] => Martin
[id_employee] => Array
    (
        [0] => 79
        [1] => 79
    )

[grabar] => si
[id] => 12
)

PHP code:

<?php
        foreach ($datos as $staff) {
?>  
<tr class="dataRow" id="soRows">
    <td>
        <input type="hidden" name="idstaff[]" value="<?php echo $staff["id_projectemployee"];?>">
        <input type="text" class="input-medium tableEmployee" value="<?php echo $staff["employee_name"];?>" name="tableEmployee"  class="tableEmployee" />
        <input type="hidden" value="<?php echo $staff["id_employee"];?>"  class="id_employee" name="id_employee[]"/>
        <button disabled="" class="deleteRow btn btn-small"><i class="icon-remove"></i></button>
    </td>
</tr>
<?php
}
?>


    $query = "update project_staff
    set
    id_employee=?
    where
    id_project=?
    and
    id_projectemployee=?
    ";

    $stmt = $this->dbh->prepare($query);

    for ($i = 0; $i < count($_POST['id_employee']); $i++){

    $employee = $_POST['id_employee'][$i];
    $idprojstaff = $_POST['idstaff'] [$i];

    $stmt->bindValue(1, $employee, PDO::PARAM_INT);
    $stmt->bindValue(2, $_POST["id"], PDO::PARAM_INT);
    $stmt->bindValue(3, $idprojstaff, PDO::PARAM_INT);

    $stmt->execute();
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 vscode的问题提问
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM