Я создал простую форму для вставки нескольких флажков с опцией, которую пользователь может указать в опции, и значения будут вставлены в 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>
<td></td>
<td><input type="submit" value="Save"> <input type="reset"></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']);
// 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
<?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
<?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");
?>
У меня 2 проблемы:
- Когда пользователь вводит такие данные:
Данные отображаются следующим образом:
Пользовательский ввод Рыбалка в опции, но данные отображаются другое .
Я не знаю, как изменить сценарий, чтобы включить значение Fishing в базу данных.
Когда пользователь хочет отредактировать вариант флажка, данные по-прежнему отображаются
другое .
Пожалуйста, помогите мне.