+ Reply to Thread
Results 1 to 2 of 2

Thread: PHP Updating Users

  1. #1
    activeradio is offline x10Hosting Member activeradio is an unknown quantity at this point
    Join Date
    Mar 2011
    Posts
    18

    PHP Updating Users

    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());
    }

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP Updating Users

    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 Code:
    <?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());
    }
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. FTP not updating...
    By mikel2k3 in forum Free Hosting
    Replies: 10
    Last Post: 04-05-2007, 08:46 AM
  2. Updating DNS
    By Rising in forum Free Hosting
    Replies: 5
    Last Post: 01-26-2006, 03:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers