PHP Updating Users

activeradio

New Member
Messages
18
Reaction score
0
Points
0
It's another PDO question. It grabs all the details from the previous page, but it cannot update the fields. Without debugging with print_r, I get the following array. Below is the code, and the table on the previous page.

Array ( [0] => 00000 [1] => [2] => ) Array ( [0] => 00000 [1] => [2] => ) Array ( [0] => 00000 [1] => [2] => ) Array ( [0] => 00000 [1] => [2] => ) Array ( [0] => 00000 [1] => [2] => ) Array ( [0] => 00000 [1] => [2] => ) Array ( [0] => 00000 [1] => [2] => ) Array ( [0] => 00000 [1] => [2] => ) Array ( [0] => 00000 [1] => [2] => )

Code:
print_r($username);
print_r($password);
print_r($email);
print_r($type);
print_r($purchased);
print_r($expiry);

Code:
<tbody>
<tr>
    <td><input name="checkbox[]" type="checkbox" value="'.$row[0].'"/></td>
    <td><input class="id" name="id[]" size="1" value="'.$row[0].'" readonly/></td>
    <td><input class="username" name="username[]" value="'.$row[1].'"/></td>
    <td><input class="password" name="password[]" type="password" maxlength=40 value="'.$row[2].'"/></td>
    <td><input class="email" name="email[]" type="email" value="'.$row[3].'"/></td>
    <td><input class="type" name="type[]" maxlength=1 value="'.$row[4].'"/></td>
    <td><input class="purchased" name="purchased[]" value="'.$row[5].'"/></td>
    <td><input class="expiry" name="expiry[]" maxlength=19 value="'.$row[6].'"/></td>
</tr>
</tbody>

Code:
$result = $db->prepare("UPDATE members SET username=:username, password=:password, email=:email, type=:type, purchased=:purchased, expiry=:expiry WHERE username=:whereusername");
for ($i = 0; $i < count($username); $i++) {    
    $result->execute(array(':username' => $username[$i], ':whereusername' => $username[$i], ':password' => $password[$i], ':email' => $email[$i], ':type' => $type[$i], ':purchased' => $purchased[$i], ':expiry' => $expiry[$i]));
    print_r($result->errorInfo());
}
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
I take it the output you're showing is generated in the for loop, rather than by the print_r($username) statement and the like. It would be helpful if you stated this explicitly to prevent confusion. Where are you setting the $username &c. variables?

Use PDOStatement::rowCount to determine whether an UPDATE changed any rows. It isn't an error if there were no matched rows, so PDOStatement::errorInfo won't reveal anything.

If you edit the username you can't use it to identify the old record in your update statement. The :username and :whereusername parameters are the same and won't match what's in the DB if changed. Use the ID field.

PHP:
<?php
$result = $db->prepare(
    "UPDATE members 
       SET username=:username, password=:password, 
           email=:email, type=:type, purchased=:purchased, 
           expiry=:expiry 
       WHERE id=:id"
    );

for ($i = 0; $i < count($username); $i++) {    
    $result->execute(array(':id' => $id[$i],
                           ':username' => $username[$i], 
                           ':password' => $password[$i],
                           ':email' => $email[$i],
                           ':type' => $type[$i], 
                           ':purchased' => $purchased[$i],
                           ':expiry' => $expiry[$i]));
    var_dump($result->errorInfo());
}
 
Top