为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
人到中年有点甜 2023-03-04 16:10:43
我创建了简单的表单来插入多个带有选项的复选框,用户可以在选项中写入这些值,这些值将插入到 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>
查看完整描述

1 回答

?
千万里不及你

TA贡献1784条经验 获得超9个赞

您需要将other数据库中的实际值保存在一个单独的字段中,并使用来自此输入的值<input type="text" id="otherValue" name="other">。然后在从数据库中检索数据的同时,检索other值,并将其显示在前端(如果选中了复选框。)

此外,您应该被告知直接使用mysqli_query,您正在向 sql 注入敞开大门。使用mysqli 准备好的语句来避免这种情况。


查看完整回答
反对 回复 2023-03-04
  • 1 回答
  • 0 关注
  • 87 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信