Programmer
Programmer

Reputation: 301

Getting and manipulating values from a table with checkboxes generated by an SQL statement

I am new to programming and I found a working code which allows me to generate a table based from a query which also contains checkboxes in each row of the resulting table. How do I get/retrieve the values from the row/s which have been selected via the checkbox?

Here's the code which generates the table:

<?php

        function SQLResultTable($Query)
        {
            $link = mysql_connect("localhost","root" , "") or die('Could not connect: ' . mysql_error());      //build MySQL Link
            mysql_select_db("myDB") or die('Could not select database');        //select database
            $Table = "";  //initialize table variable

            $Table.= "<table border='1' style=\"border-collapse: collapse;\">"; //Open HTML Table

            $Result = mysql_query($Query); //Execute the query
            if(mysql_error())
            {
                $Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
            }
            else
            {
                //Header Row with Field Names
                $NumFields = mysql_num_fields($Result);
                $Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\">";
                for ($i=0; $i < $NumFields; $i++)
                {
                    $Table.= "<th>" . mysql_field_name($Result, $i) . "</th>";
                     if($i==$NumFields-1)
                     {
                     $Table.= "<th>&nbsp&nbspSelect&nbsp&nbsp</th>";
                     $Table.= "</tr>";
                     }
                }


                //Loop thru results
                $RowCt = 0; //Row Counter
                while($Row = mysql_fetch_assoc($Result))
                {

                    //Alternate colors for rows
                    if($RowCt++ % 2 == 0) $Style = "background-color: #FFCCCC;";
                    else $Style = "background-color: #FFFFFF;";

                    $Table.= "<tr style=\"$Style\">";
                    //Loop thru each field
                    foreach($Row as $field => $value)
                    {

                        $Table.= "<td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp$value&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</td>";

                    }
                    //$Table.= "<td><input type='checkbox'></td>";
                    $Table.= "<td><input type='checkbox'></td>";
                    $Table.= "</tr>";
                }

            }
            $Table.= "</table>";

            return $Table;
        }
?>

Upvotes: 0

Views: 297

Answers (1)

A good starting point would be to read the information on how to select information from a database. With SQL generally this is done using SELECT statements. The MySQL SELECT documentation can be found here

http://dev.mysql.com/doc/refman/5.0/en/select.html

Using Google you should be able to find information regarding how to use a SELECT with MySQL and PHP. Once you've had a go, and if you still can't figure out where you are going wrong, then post up a new question with specific information as to your problem and example code, and then I'm sure others will be able to provide you with advice relating to that specific problem.

IMPORTANT

As a security point of view, NEVER include usernames or passwords in your source code examples. Just put in placeholder values instead.

Upvotes: 1

Related Questions