PHP 在 while 循环内使用动态表发布到 mysql

问题描述 投票:0回答:1

在一些帮助下,我制作了一个卡车检查表,其中包含通过、警告和失败的按钮。还有一个评论按钮,可以添加一行评论。这一切都是在 mysqli_fetch_array 的 while 循环中完成的。

如何将填写的表单中的数据发布到 mysql 数据库?我没有太多使用动态表循环发布到 mysql 的经验。

Example of my form

这是我的代码:

<div class="container-xxl flex-grow-1 container-p-y">
             
<h2 align="center"><?php echo $appname?></h2><br />
   <div class="form-group">
   <form name="add_name" id="add_name">
                    
   <div class="table-responsive">
      <table class="table table-bordered" id="dynamic_field">

   <?php while ($checks = mysqli_fetch_array($checksqry)) {
            echo '<tr>';
            echo '<td style="width: 20%">'.$checks['check_name'].' <br><br> 
            <input type="radio" class="btn-check" name="btn[' . $checks['id'] . ']" id="btnpass[' . $checks['id'] . ']" value="Pass">
            <label class="btn rounded-pill btn-outline-success" for="btnpass[' . $checks['id'] . ']">Pass</label>&Tab;&Tab;

           <input type="radio" class="btn-check" name="btn[' . $checks['id'] . ']" id="btnwarning[' . $checks['id'] . ']" value="Warning">
           <label class="btn rounded-pill btn-outline-warning" for="btnwarning[' . $checks['id'] . ']">Warning</label>&Tab;&Tab;

           <input type="radio" class="btn-check" name="btn[' . $checks['id'] . ']" id="btnfail[' . $checks['id'] . ']" value="Fail">
           <label class="btn rounded-pill btn-outline-danger" for="btnfail[' . $checks['id'] . ']">Fail</label>&Tab;&Tab;

           <button onclick="myFunction('. $checks['id'] . ')" type="button" name="comment[' . $checks['id'] . ']" id="comment[' . $checks['id'] . ']" class="btn btn-info" style="float: right">Click</button> 
<br><br>
           <div style="display:none;" id="commentlinediv[' . $checks['id'] . ']">
           <input type="text" class="text-line" placeholder="Type Comment Here" id="commentline[' . $checks['id'] . ']"/>
</div>
</td>';
echo '<tr>';}?>

 </table>
<input type="button" name="submit" id="submit" class="btn btn-info" value="Submit" />
</div>
</form>
</div>
</div>

<style>
input[type="text"] {
   border:none; /* Get rid of the browser's styling */
   border-bottom:1px solid black; /* Add your own border */
}

.text-line {
   background-color: transparent;
   color: black;
   outline: none;
   outline-style: none;
   border-top: none;
   border-left: none;
   border-right: none;
   border-bottom: solid #eeeeee 1px;
   padding: 3px 10px;
   width: 300px;
}
                


</style>


<script>
function myFunction(var1) {
  
document.getElementById("commentlinediv[" + var1 + "]").style.display = "inline";
}

</script>
javascript php mysql mysqli
1个回答
0
投票
Based on your description I modify small parts of your code in order to handle to posted data from your form and then do your sql action(mean insert, update or delete).
First you have to add this php condition at first of your code and then modify the <form> html tag to the written code.

>   <?php
    if(isset($_POST['submit'])){
    // Here you can get your posted data
    // Then you have to write a query to insert, update or whatever action you need to do with your data
    }
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="container-xxl flex-grow-1 container-p-y">

        <h2 align="center"><?php echo $appname ?></h2><br />
        <div class="form-group">
            <form name="add_name" id="add_name" method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">

                <div class="table-responsive">
                    <table class="table table-bordered" id="dynamic_field">

                        <?php while ($checks = mysqli_fetch_array($checksqry)) {
                            echo '<tr>';
                            echo '<td style="width: 20%">' . $checks['check_name'] . ' <br><br> 
                         <input type="radio" class="btn-check" name="btn[' . $checks['id'] . ']" id="btnpass[' . $checks['id'] . ']" value="Pass">
                         <label class="btn rounded-pill btn-outline-success" for="btnpass[' . $checks['id'] . ']">Pass</label>&Tab;&Tab;
             
                        <input type="radio" class="btn-check" name="btn[' . $checks['id'] . ']" id="btnwarning[' . $checks['id'] . ']" value="Warning">
                        <label class="btn rounded-pill btn-outline-warning" for="btnwarning[' . $checks['id'] . ']">Warning</label>&Tab;&Tab;
             
                        <input type="radio" class="btn-check" name="btn[' . $checks['id'] . ']" id="btnfail[' . $checks['id'] . ']" value="Fail">
                        <label class="btn rounded-pill btn-outline-danger" for="btnfail[' . $checks['id'] . ']">Fail</label>&Tab;&Tab;
             
                        <button onclick="myFunction(' . $checks['id'] . ')" type="button" name="comment[' . $checks['id'] . ']" id="comment[' . $checks['id'] . ']" class="btn btn-info" style="float: right">Click</button> 
             <br><br>
                        <div style="display:none;" id="commentlinediv[' . $checks['id'] . ']">
                        <input type="text" class="text-line" 
    placeholder="Type Comment Here" id="commentline[' . $checks['id'] . 
    ']"/>
             </div>
             </td>';
                            echo '<tr>';
                        } ?>

                    </table>
                    <input type="button" name="submit" id="submit" class="btn btn-info" value="Submit" />
                </div>
            </form>
        </div>
    </div>

    <style>
        input[type="text"] {
            border: none;
            /* Get rid of the browser's styling */
            border-bottom: 1px solid black;
            /* Add your own border */
        }

        .text-line {
            background-color: transparent;
            color: black;
            outline: none;
            outline-style: none;
            border-top: none;
            border-left: none;
            border-right: none;
            border-bottom: solid #eeeeee 1px;
            padding: 3px 10px;
            width: 300px;
        }
    </style>


    <script>
        function myFunction(var1) {

            document.getElementById("commentlinediv[" + var1 + "]").style.display = "inline";
        }
    </script>
        </body>
    </html>

Hope this code helps you to solve your problem.
Good Luck!
© www.soinside.com 2019 - 2024. All rights reserved.