mysql-pdo基于下拉列表选择值更新表

idv4meu8  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(271)

我完全搞不懂。我通过从数据库获取数据并填充下拉列表和表来填充我的表。我需要获取下拉列表的值,一旦选择了该值,我需要通过另一个mysql命令来更新表以获取数据。我不知道如何获取下拉列表值来更改mysql查询和更新表。我的代码在下面。

<?php
session_start();
include '/Header.php';
include '/Footer.php';
include '/Functions.php';
extract($_POST); 

$dbConnection = parse_ini_file("/db_connection.ini");
extract($dbConnection);
$myPdo = new PDO($dsn, $user, $password);
$myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

function fill_drp($myPdo) {
    $output = '';
    $query = $myPdo->prepare("SELECT Term, Year FROM Semester");
    $query->execute();

    foreach ($query->fetchall(PDO::FETCH_ASSOC) as $row) {
       $output .= '<option value="' . $row['Year'] . ' ' . $row['Term'] . '">' . $row['Year'] . ' ' . $row['Term'] . '</option>';

        #$year = substr(($row['Year'] . $row['Term']), 2, 3); #need it to be 18W instead to query database
    }
    return $output;
}

function fill_table($myPdo, $selected_term) {
    $output = '';
    $query = $myPdo->prepare("select CourseOffer.CourseCode, 
                            Title, WeeklyHours from CourseOffer join Course 
                            on CourseOffer.CourseCode = Course.CourseCode 
                            where SemesterCode= '" . $selected_term . "'");
    $query->execute();
    foreach ($query->fetchall(PDO::FETCH_ASSOC) as $row) {
        $cc = $row['CourseCode'];
        $output.= "<tr>";
        $output.= "<td>" . $row['CourseCode'] . "</td>";
        $output.= "<td>" . $row['Title'] . "</td>";
        $output.= "<td> " . $row['WeeklyHours'] . "</td>";
        $output.="<td> <input type='checkbox' name='chk[]' value='$cc'" . "</td>";
        $output.= "</tr>";
    }
    return $output;
}
?>

<html>
    <head>
        <title>Online Course Registration</title>
        <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
    </head>
    <body>
        <h1 class="text-center">Course Selection</h1>
        <p>Welcome <strong><?php echo $_SESSION['name']; ?></strong> (not you? change user <a href="Login.php">here</a>)</p>
        <p>You have registered <strong>#todo</strong> hours of course(s) for the semester</p>
        <p>You can register <strong>#todo</strong> more hours of course(s) for the semester</p>
        <p>Please note that the courses you have registered will not be displayed in the list</p>

        <form action="CourseSelection.php" method="post">
            <br/>
            <div class="dropdown text-right">
                <select class="dropdown" id="terms" name="terms" >
                    <?php
                    echo fill_drp($myPdo);
                    ?>
                </select> 
            </div> 
            <div id="tableContainer"> 
                <table border="1" class="table" id="table1">
                    <thead class="thead-light">
                        <tr>
                            <th scope="col" >Code</th>
                            <th scope="col"> Course Title</th>
                            <th scope="col">Hours</th>
                            <th  scope="col">Select</th>

                        </tr>
                        <?php
                        if ($_POST["term"] != null) {
                            $_SESSION['term'] = $_POST["term"];#attempt at getting selected dropdown value.
                        }
                        if (isset( $_SESSION['term'])) {
                            $selected_val = substr(($_SESSION['term']), 2, 3);
                            echo fill_table($myPdo, $selected_val); 
                        } else {
                            echo fill_table($myPdo, '17F');#default is 17F but could be 18W 19S etc..
                        }
                        ?>
                </table>
            </div>
            <br/>
            <input type='submit'  class="btn btn-primary"  class='button' name='submit' value='submit'/>
        </form>
    </body>

</html>

ajax组件
我在table下面加了这个脚本

<script>  
        $(document).ready(function(){  
             $('#terms').change(function(){  
                  var term = (($(this).val()).replace(' ','')).substring(2, 5);  
                  //var term = '19W';
                  console.log(term); //shows that it's getting the semesters
                  $.ajax({  
                       url:"RefreshTable.php",  
                       method:"POST",  
                       data:{term:term},  
                       success:function(data){  
                            $('#tableContainer').html(data);  
                       }  
                  });  
             });  
        });  
        </script>

我的refreshtable.php包含以下内容

<?php
$dbConnection = parse_ini_file("/db_connection.ini");
extract($dbConnection);
$myPdo = new PDO($dsn, $user, $password);
$myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$output = '';
if (isset($_POST["term"])) {
    if ($_POST["term"] != '') {
        $query = $myPdo->prepare("select CourseOffer.CourseCode, 
                            Title, WeeklyHours from CourseOffer join Course 
                            on CourseOffer.CourseCode = Course.CourseCode 
                            where SemesterCode= '".$_POST["term"]."'");
    } else {
        $query = $myPdo->prepare("select CourseOffer.CourseCode, 
                            Title, WeeklyHours from CourseOffer join Course 
                            on CourseOffer.CourseCode = Course.CourseCode 
                            where SemesterCode= '17F'");
    }
    $query->execute();
    foreach ($query->fetchall(PDO::FETCH_ASSOC) as $row) {
        $cc = $row['CourseCode'];
        $output.= "<tr>";
        $output.= "<td>" . $row['CourseCode'] . "</td>";
        $output.= "<td>" . $row['Title'] . "</td>";
        $output.= "<td> " . $row['WeeklyHours'] . "</td>";
        $output.="<td> <input type='checkbox' name='chk[]' value='" . $row['CourseCode'] . "'" . "</td>";
        #(isset($copies) ? $copies[$i] : '') . "' ></td>";
        $output.= "</tr>";
    }
    echo $output;
}
?>
w1e3prcc

w1e3prcc1#

你可以像他们在这里做的那样,用同样的方法来选择。
textbox onchange调用php函数
使用onchange调用函数执行ajax调用并调用php脚本。

ctehm74n

ctehm74n2#

您需要在 fill_drp() 功能。
像这样:

$output .= '<option value="' . $row['Year'] . ' ' . $row['Term'] . '">' . $row['Year'] . ' ' . $row['Term'] . '</option>';

相关问题