Jump to content

Archived

This topic is now archived and is closed to further replies.

molly31

selecting records by using checkbox

Recommended Posts

I have a database already set up and want to display records from it on another page, selected by 'ticking' a checkbox

How do i do it?

Thanks in advance for any help

Share this post


Link to post
Share on other sites
This will print off all records in the database, along with column names in a single table. It will also place a checkbox at the beginning of each row. You will have to wrap it in your form tags. When it is submittted, you will have in $_POST all of the checkboxes and the ones that are selected will have a value of "on".

Do a print_r on $_POST to see what the result is.

You will also have to supply the db connection and the tablename as $tablename.

[code]
<?php
$query = "SHOW COLUMNS FROM tablename";
$result = mysql_query($query);

$html = '
    <table>
        <tr>';
    
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    if ($row['Key'] == "PRI") {
        $primarykey = $row['Field'];
    }
    
    $html .= "<th>" . $row['Field'] . "</th>";
}

$html .= "
        </tr>";

$query = "SELECT * FROM tablename";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $html .= "<tr><td><input type=\"checkbox\" name=\"" . $row[$primarykey] . "\"></td>";
    foreach ($row as $value) {
        $html .= "<td>$value</td>";
    }
    $html .= "</tr>";
}
$html .= "
    </table>";[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.