如何插入和编辑带有选项的多复选框,用户可以在PHP脚本中编写内容?
我创建了一个简单的表单来插入多个复选框,其中包含用户可以在选项中写入的选项,这些值将插入mysql 这是我的密码:如何插入和编辑带有选项的多复选框,用户可以在PHP脚本中编写内容?,php,html,Php,Html,我创建了一个简单的表单来插入多个复选框,其中包含用户可以在选项中写入的选项,这些值将插入mysql 这是我的密码: index.php 身份证件 名称 爱好 行动 add.html 身份证件 名称 爱好 阅读 视频游戏 其他 add.php 霍比 >视频游戏 update.php 您需要将数据库中其他的实际值保存在一个单独的字段中,并使用此输入的值。 然后,在从数据库检索数据时,检索其他值,并将其显示在前端(如果选中复选框) 此外,您应该被告知,直接使用mysqli\u quer
身份证件
名称
爱好
行动
身份证件
名称
爱好
阅读
视频游戏
其他
霍比
>视频游戏
您需要将数据库中其他
的实际值保存在一个单独的字段中,并使用此输入的值
。
然后,在从数据库检索数据时,检索其他
值,并将其显示在前端(如果选中复选框)
此外,您应该被告知,直接使用mysqli\u query
,您正在向sql注入敞开大门。使用可避免这种情况。您需要将数据库中的其他
的实际值保存在一个单独的字段中,并使用此输入的值
。
然后,在从数据库检索数据时,检索其他
值,并将其显示在前端(如果选中复选框)
此外,您应该被告知,直接使用mysqli\u query
,您正在向sql注入敞开大门。使用以避免出现这种情况。所以,您的意思是我必须在数据库中创建新字段以保存其他字段的值?好的,我试试。是的。在显示嗜好时,使用该值来显示,而不是other
。所以,您的意思是我必须在数据库中创建新字段来保存other的值?好的,我试试。是的。在显示嗜好时,使用该值来显示,而不是other
。
<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>
<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>
<td></td>
<td><input type="submit" value="Save"> <input type="reset"></td>
</tr>
</table>
</form>
<?php
// connect to database
include 'conn.php';
// capture data from form
$id = $_POST['id'];
$name = $_POST['name'];
$name = htmlspecialchars($name, ENT_QUOTES);
$hobby = implode(", ", $_POST['hobby']);
// input data to database
mysqli_query($conn,"insert into checkbox(id,name,hobby) values('$id','$name','$hobby')") or die(mysqli_error($conn));
// redirect page to index.php
header("location:index.php");
?>
<?php
include 'conn.php';
$id = $_GET['id'];
$query = mysqli_query($conn,"select * from checkbox where id='$id'");
while($data = mysqli_fetch_array($query)){
$datahobby=explode(', ', $data['hobby']);
?>
<form method="post" action="update.php">
<table>
<tr>
<td width="60px" valign="top">Hobi</td>
<td valign="top">
<label><input type="checkbox" name="hobby[]" value="Reading" <?php if (in_array("Reading", $datahobby)) echo "checked";?> >Reading</label><br>
<label><input type="checkbox" name="hobby[]" value="Video Gaming" <?php if (in_array("Video Gaming", $datahobby)) echo "checked";?> >Video Gaming</label><br>
<label><input type="checkbox" name="hobby[]" value="other" <?php if (in_array("other", $datahobby)) echo "checked";?> >Other</label>
<input type="text" id="otherValue" name="other">
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Update"> <button onclick="goBack()">Go Back</button></td>
</tr>
</table>
</form>
<?php
}
?>
<?php
// connect to database
include 'conn.php';
// capture data from form
$id = $_POST['id'];
$name = $_POST['name'];
$name = htmlspecialchars($name, ENT_QUOTES);
$hobby = implode(",", $_POST['hobby']);
// update data ke database
mysqli_query($conn,"update checkbox set name='$name', hobby='$hobby', where id='$id'");
// redirect page to index.php
header("location:index.php");
?>