PHP Table

activeradio

New Member
Messages
18
Reaction score
0
Points
0
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:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Code:
$memberQuery->fetch();

should be
Code:
 $memberQuery1->fetch();
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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:
$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:
<?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>
 

activeradio

New Member
Messages
18
Reaction score
0
Points
0
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:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Toss in:

echo "session variable is: " . $_SESSION['username'] ;
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
PHP:
<?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:

activeradio

New Member
Messages
18
Reaction score
0
Points
0
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:

activeradio

New Member
Messages
18
Reaction score
0
Points
0
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:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Note that "essentially" is quite different from "exactly" when it comes to programs; a single character can make a huge difference. We need to see a complete, concise, representative code sample. Otherwise, it's impossible to say with any certainty why the code doesn't generate any input elements.
 
Top