vendredi 9 décembre 2016

How do I associate data from a mysql query with checkboxes in a table with php?

I have created a webpage that I am using to display data from a bug report database. The data from the table is being displayed on the page in a table using a foreach loop. After each row is created a final column is created containing a checkbox. What I am trying to do is click on a checkbox or multiple checkboxes and then click a button that will then modify a mysql table through an update string that will assign the bug report to a technician.

I need to connect the data in the webpage table to the checkbox so that I only assign the bugs that are selected to the technician selecting them. I'm open to using php or javascript to accomplish this.

Here is my code:

<div class="wrapper row3">
 <div id="container" class="clear"> 

  <div id="content"> 
   <section>
   <?php

        //Connection Test to verify connection

        //$link = mysql_connect('dev.cictspace.net', 'CSC272_BDJ', 'BDJ_FALL2016');
        //  if (!$link) {
        //  die('Could not connect: ' . mysql_error());
        //  }
        //  echo 'Connected successfully';
        //  mysql_close($link);

        $host="dev.cictspace.net:3306";
        $username="CSC272_BDJ";
        $password="BDJ_FALL2016";
        $db_name="CSC272_SLEEPY_STUDENTS";
        $tbl_name="Bug_Assignment_Table";

        $query2 = "SELECT `BugID`, `BugReportDate`, `Software_Name`, `Software_Type`, `Bug_Description`, `Reported_By` FROM `Bug Report Table` WHERE `Bug Report Table`.`Bug_Assigned` <> 'Yes' OR `Bug_Assigned` IS NULL;";


        if (!($connection = mysql_connect( $host, $username, $password)))
            die( "Could not connect to server");

        if ( !mysql_select_db( $db_name, $connection))
            die( "Could not open database");

        if (!( $result2 = mysql_query($query2, $connection)))
            die("Could not run query");
        //Open database



        ?>

        <h1>Uassigned Bug Reports<h1>
        <table border = "1" cellpadding = "3" cellspacing = "2" >
            <thead>
                <tr>
                    <th>Bug ID</th>
                    <th>Bug Reported Date</th>
                    <th>Software Name</th>
                    <th>Software Type</th>
                    <th>Bug Description</th>
                    <th>Reported By:</th>
                    <th>Assign to Tech</th>
                </tr>
            </thead>

            <tbody> 
        <?php

        // fetch each record in result set
        for ( $counter = 0; $row = mysql_fetch_row( $result2 ); $counter++ )
        {

           // build table to display results
           print( "<tr>" );

           foreach ( $row as $key => $value ) 
              print( "<td>$value</td>" );
              print( "<td align='center'><input type='checkbox' name='Assigned$counter' value='$counter'></td>");

           print( "</tr>" );


        }

        mysql_close( $database );
        ?>


            </tbody>
        </table>
        <p>
            <button type="button" onclick="post_tech.php">Save Assignment</button>
        </p>
      </section>
    </div>
  </div>
</div>

I've reached a point where I am stuck. I can pass the variable that represents each checkbox to another php page using $_SESSION or $_POST but I'm not certain how to extract the data from the table and associate it with the checkbox so that I update the right record.

Aucun commentaire:

Enregistrer un commentaire