Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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();
share|improve this question
    
I think you didnt see the Pastie code pastie.org/5480319 –  user1763639 Dec 4 '12 at 19:27
    
It is possible to do so using mysql_* but there are limitations. If you want to update multiple rows to something else.. All the same value/string, then you could use a while loop; else.. you could create a foreach loop, and give parameters for each of the array values. –  Daryl Gill Dec 4 '12 at 19:30
    
Im using PDO in order to do the update..THIS IS THE CODE pastie.org/5480319.. also as you can see there im looping around the query. AS I HAVE MORE THAN ONE VALUE IN id_projectemployee. I NEED TO KNOW HOW TO MATCH EACH VALUE IN THE CORRECT MEMBER. Other than that my code is only updating the last row.. please see the code above –  user1763639 Dec 4 '12 at 19:36
    
What is the value of $_POST['id_employee']? What is the value of count($_POST['id_employee'])? –  AllInOne Dec 4 '12 at 19:56
    
¿? I have printed them out.. –  user1763639 Dec 4 '12 at 20:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.