【问题标题】:Updating table based on dropdown list selected value via PHP mysql PDO通过 PHP mysql PDO 根据下拉列表选择的值更新表
【发布时间】:2018-11-25 08:01:24
【问题描述】:

我根本想不通。我通过从数据库中获取数据并填充下拉列表和表格来填充我的表格。我需要获取下拉值,一旦选择了该值,我需要通过获取数据的不同 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 组件

我在我的桌子正下方添加了这个脚本

   <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;
}
?>

【问题讨论】:

    标签: php mysql pdo


    【解决方案1】:

    你可以像他们在这里做的那样做,使用 select 的方式相同。
    textbox onchange call php function
    使用 onchange 调用函数执行 Ajax 调用并调用您的 php 脚本。

    【讨论】:

    • 问题是我不知道如何使用ajax。我花了过去 10 个小时试图让它与 ajax 一起工作,但无济于事。我的表根本不会更新,我仍然会得到第一页加载的初始值。我将使用我的 ajax 尝试更新代码。
    【解决方案2】:

    您需要在fill_drp() 函数的下拉列表中为您的选项分配一个值。

    像这样:

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

    【讨论】:

    • 已更新问题但仍无法在下拉更改时更新表格
    • 我在 ajax 的 term 变量上看到了一些字符串操作。您需要什么值才能使查询正常工作?
    • 输出将类似于“2018 Winter”或“2019 Summer”我需要它们为 18W 或 19S 而不是字符串操作所做的
    • 然后我可以查询数据库添加where SemesterCode = 18W 等。
    • 祝你项目的其余部分好运。干杯!
    猜你喜欢
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多