更新多行php mysql

时间:2012-12-04 19:22:13

标签: php mysql

我尝试更新以下代码http://pastie.org/5480319中的多行。问题是我在WHERE子句中有多个id_projectemployee,所以我需要匹配正确成员中的每个值。到目前为止,我的代码只更新了最后一行,并为id_employee设置了相同的值,如下面的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代码:

<?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 个答案:

没有答案