+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11
Like Tree2Likes

Thread: PHP Table

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

    PHP Table

    I basically have a table in my Admin CP, which is suppose to display the contents from all my columns, but of course it doesn't provide me with any errors. When I implement debugging, it says:

    Array ( [0] => 00000 )
    This is what I have in the first part of my code:

    Code:
    $memberQuery1 = $db->prepare("SELECT * FROM members");
    $memberQuery1->execute();
    $result = $memberQuery1->setFetchMode(PDO::FETCH_NUM);
    Code:
    <?php
        while($row = $memberQuery->fetch())
        {
        echo '
            <tbody>
            <tr>
                <td><input name="checkbox[]" type="checkbox" value="'.$row[0].'"></td>
                <td><input class="id" name="id[]" type="text" size="1" value="'.$row[0].'" readonly></td>
                <td><input class="username" name="username[]" type="text" 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="text" value="'.$row[3].'"></td>
                <td><input class="type" name="type[]" type="text" maxlength=1 value="'.$row[4].'"></td>
                <td><input class="purchased" name="purchased[]" type="text" value="'.$row[5].'"></td>
                <td><input class="expiry" name="expiry[]" type="text" maxlength=19 value="'.$row[6].'"></td>
            </tr>
            </tbody>';
        }
    
        //Debugging
        print_r($row);
        print_r($memberQuery1->errorInfo());
        //End
    ?>
    The table appears empty empty, and there is one row in the database.
    Last edited by activeradio; 06-27-2011 at 11:23 AM.
    dinomirt96 and karimirt47 like this.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: PHP Table

    Code:
    $memberQuery->fetch();
    should be
    Code:
     $memberQuery1->fetch();
    Last edited by descalzo; 06-27-2011 at 12:01 PM.
    Nothing is always absolutely so.

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

    Re: PHP Table

    Don't use SELECT *; select only the columns you need. When paired with the numeric fetch mode it's particularly problematic, as changing the table structure will wreck the result processing.

    "text" is the default input type; there's no need to specify it.

    A definition list seems more appropriate than a table here. If you were editing multiple entries, then the two dimensional structure of a table would make sense.

    All elements should be closed. That means <input .../>, not <input ...>

    Form inputs should have a corresponding <label> element.

    There's no need to concat strings when echoing them; echo is variadic, echoing every argument. A heredoc would be more readable than using singly quoted strings.
    PHP Code:
    $memberQuery1->setFetchMode(PDO::FETCH_ASSOC);
    foreach (
    $memberQuery1 as $row) {
        echo <<<EOS
            <dl>
                <dt><label for="checkbox">???</label></dt>
                <dd><input name="checkbox[]" type="checkbox" value="
    $row[id]" /></dd>
                
                <dt><label for="id">ID</label></dt>
                <dd><input class="id" name="id[]" size="1" value="
    $row[id]" readonly></dd>
                
                <dt><label for="username">Username</label></dt>
                <dd><input class="username" name="username[]" value="
    $row[username]"></dd>
                
                <dt><label for="password">Password</label></dt>
                <dd><input class="password" name="password[]" type="password" maxlength=40 value="
    $row[password]'"></dd>
                
                <dt><label for="email">Email</label></dt>
                <dd><input class="email" name="email[]" value="
    $row[email]"></dd>
                
                <dt><label for="type">Type</label></dt>
                <dd><input class="type" name="type[]" maxlength=1 value="
    $row[type]"></dd>
                
                <dt><label for="purchased">Purchased</label></dt>
                <dd><input class="purchased" name="purchased[]" value="
    $row[purchased]"></td>
                
                <dt><label for="expiry">Expiry</label></dt>
                <dd><input class="expiry" name="expiry[]" maxlength=19 value="
    $row[expiry]"></td>
            </dl>
    EOS;
    }
    ... 
    Or output the table cells in a loop:
    PHP Code:
    <?php
    $attrs 
    = array(
        
    'id' => ' size="1" readonly',
        
    'username' => '',
        
    'password' => ' type="password" maxlength="40"',
        
    'email' => '',
        
    'type' => ' maxlength="1"',
        
    'purchased' => '',
        
    'expiry' => ' maxlength="19"'
        
    );
    ...
    ?>
    <dl>
      <?php foreach ($memberQuery1 as $row) { ?>
        <dt><label for="checkbox">???</label></dt><input name="checkbox[]" type="checkbox" value="<?php echo $row['id']; ?>"></dd>
        <?php foreach ($row as $key => $val) { ?>
          <dt><label for="<?php echo $key ?>"><?php echo ucwords($key); ?></label></dt>
          <dd><input class="<?php echo $key ?>" name="<?php echo $key?>[]" value="<?php echo $val?><?php echo $attrs[$key?></dd>
        <?php ?>
      <?php ?>
    </dl>
    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.

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

    Re: PHP Table

    Going back to the tables, I couldn't get much when debugging this problem. I changed the prepare statement to this:

    Code:
    $memberQuery = $db->prepare("SELECT * FROM members WHERE username=:username");
    $memberQuery->execute(array(':username' => $_SESSION['username']));
    $result = $memberQuery->setFetchMode(PDO::FETCH_NUM);
    I tried debugging in various ways:

    Code:
    $memberQuery = $db->prepare("SELECT * FROM members WHERE username=:username");
    try {
    	$memberQuery->execute(array(':username' => $_SESSION['username']));
    	$result = $memberQuery->setFetchMode(PDO::FETCH_NUM);
    }
    catch (PDOException $e) {
      print $e->getMessage();
    }
    
    $memberQuery->debugDumpParams();
    print_r($memberQuery->errorInfo());
    error_reporting(E_ALL);
    This is all that was returned:

    Code:
    SQL: [46] SELECT * FROM members WHERE username=:username Params: 1 Key: Name: [9] :username paramno=-1 name=[9] ":username" is_param=1 param_type=2 Array ( [0] => 00000 [1] => [2] => )
    I cannot figure out what the problem is. Perhaps it is some logic behind PDO, mySQL or PHP?
    Last edited by activeradio; 08-03-2011 at 10:45 AM.

  5. #5
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: PHP Table

    Toss in:

    echo "session variable is: " . $_SESSION['username'] ;
    Nothing is always absolutely so.

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

    Re: PHP Table

    It says this:

    Code:
    session variable is: chris

  7. #7
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: PHP Table

    PHP Code:



    <?php
    // CONNECTION STUFF 
    echo "<pre>" ;

    try {
        
    $db = new PDO("mysql:host=$host;dbname=$db"$user$pass);

      
    $memberQuery $db->prepare("SELECT * FROM mesa WHERE username=:username");
      
    $memberQuery->execute(array('username' => 'my name'));
      
    $result $memberQuery->setFetchMode(PDO::FETCH_NUM);

        while(
    $row $memberQuery->fetch()){
                echo 
    $row[0] . "  -   " $row[1] .  "  -  " $row[2] . "\n" ;
        }

        
    $dbh null;
    } catch (
    PDOException $e) {
        print 
    "Error!: " $e->getMessage() . "<br/>";
        die();
    }

    echo 
    "</pre>";
    Just ran the above code against a test DB and it worked.

    Make sure (PHPMyAdmin) that the entry in the db is exactly as you expect. That includes spaces. How is the username field defined?
    Last edited by descalzo; 08-03-2011 at 07:39 PM.
    Nothing is always absolutely so.

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

    Re: PHP Table

    The code you gave me worked. In terms of the table, this is essentially what I have, but the input boxes are not displaying.
    Last edited by activeradio; 08-03-2011 at 09:46 PM.

  9. #9
    tarkeshwarraj38 is offline x10Hosting Member tarkeshwarraj38 is an unknown quantity at this point
    Join Date
    Aug 2011
    Posts
    1

    Re: PHP Table

    nice dude

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

    Re: PHP Table

    There must be a reason as to why this isn't working. In case you were confused earlier, it worked when I echoed the rows into a query. When I use input boxes in a table, it doesn't.. Now this shouldn't be a problem because it's pretty much the same table, only with a different query.
    Last edited by activeradio; 08-06-2011 at 04:27 AM.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. DIV as Table?
    By Twinkie in forum Programming Help
    Replies: 3
    Last Post: 09-29-2008, 11:11 PM
  2. MysQL Query for Master table & child table
    By phpasks in forum Programming Help
    Replies: 8
    Last Post: 08-07-2008, 08:07 AM
  3. sql table
    By Anonymousx1 in forum Free Hosting
    Replies: 1
    Last Post: 02-01-2008, 10:13 PM
  4. Table inside table
    By wizeman in forum Tutorials
    Replies: 4
    Last Post: 07-11-2005, 05:56 PM
  5. Table
    By DaanG in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 04-01-2005, 03:34 AM

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