如何使用用户可以在 PHP 脚本中编写内容的选项插入和编辑多复选框?

我创建了简单的表单来插入多个带有选项的复选框,用户可以在选项中写入这些值,这些值将插入到 mysql 中。


这是我的代码:


索引.php

        <a href="add.html">+ ADD PEOPLE</a>

        <br/>

        <br/>

        <table border="1">

                <tr>

                        <th>ID</th>

                        <th>Name</th>

                        <th>Hobby</th>

                        <th>Action</th>

                </tr>

                <?php

                include 'conn.php';

                $no = 1;

                $query = mysqli_query($conn,"select * from checkbox ");


                while($data = mysqli_fetch_array($query)){

                        ?>

                        <tr>

                                <td><?php echo ($data['id']); ?></td>

                                <td><?php echo nl2br($data['name']); ?></td>

                                <td><?php echo nl2br ($data['hobby']); ?></td>

                                <td>

                                        <a href="edit.php?id=<?php echo $data['id']; ?>">EDIT</a>

                                        <a href="remove.php?id=<?php echo $data['id']; ?>" onclick="return checkDelete()">REMOVE</a>

                                </td>

                        </tr>

                        <?php

                }

                ?>

        </table>

添加.html

 <p><a href="index.php">Back</a>

 <form action="add.php" method="post">

  <table cellpadding="3" cellspacing="0">


   <tr>

    <td>ID</td>

    <td><input type="text" name="id" required></td>

   </tr>


   <tr>

    <td>Name</td>

    <td><input type="text" name="name" size="30" required></td>

   </tr>


   <tr>

        <td width="60px" valign="top">Hobby</td>

        <td valign="top">

         <label><input type="checkbox" name="hobby[]" value="Reading">Reading</label><br>

         <label><input type="checkbox" name="hobby[]" value="Video Gaming">Video Gaming</label><br>

         <label><input type="checkbox" name="hobby[]" value="other">Other</label>

                <input type="text" id="otherValue" name="other">


        </td>

       </tr>



 <tr>



人到中年有点甜
浏览 93回答 1
1回答

千万里不及你

您需要将other数据库中的实际值保存在一个单独的字段中,并使用来自此输入的值<input type="text" id="otherValue" name="other">。然后在从数据库中检索数据的同时,检索other值,并将其显示在前端(如果选中了复选框。)此外,您应该被告知直接使用mysqli_query,您正在向 sql 注入敞开大门。使用mysqli 准备好的语句来避免这种情况。
打开App,查看更多内容
随时随地看视频慕课网APP