我目前正在创建一个带有复选框的表单。在我工作的那一刻,它只是将最后选择的复选框添加到数据库表中。我需要它将所有选定的值添加到表中。我想我可能需要使用内爆,但我不确定在哪里放。任何帮助将不胜感激。
的 actionplan.php 的
<h1>My Action Plan</h1>
<?php
if (isset($_GET['success']) && empty($_GET['success'])) {
} else {
if (empty($_POST) === false && empty($errors) === true) {
$actionplan = array(
'studentid' => $user_data['studentid'],
'interests_hobbies' => $_POST['interests_hobbies'],
);
actionplan($actionplan);
header ('Location: actionplan.php');
exit();
} else if(empty($errors) === false){
echo output_errors($errors);
}
?>
<form action="" method="post">
<li>
<p class="p4">
What are your interests and hobbies?*
<center>
<table border="0">
<tr>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Animal Care"/>Animal Care</td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Computer Games"/>Computer Games </td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Gardening"/>Gardening</td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Internet Browsing"/>Internet Browsing</td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Sculpting"/>Sculpting</td>
</tr>
<tr>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Bird Watching "/>Bird Watching</td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Fishing"/>Fishing</td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Golfing"/>Golfing </td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Painting"/>Painting </td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Social Networking"/>Social Networking</td>
</tr>
<tr>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Bowling"/>Bowling</td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Food Tasting"/>Food Tasting </td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Gymnastics"/>Gymnastics </td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Reading"/>Reading </td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Watching Movies"/>Watching Movies</td>
</tr>
<tr>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Camping"/>Camping</td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Football"/>Football </td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Interior Design"/>Interior Design </td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Surfing"/>Surfing </td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Yoga"/>Yoga</td>
</tr>
</table>
<li>
<input type="submit" value="Submit">
</li>
</ul>
</form>
的 users.php 的
function actionplan($actionplan) {
array_walk($actionplan, 'array_sanitize');
$fields = '`' . implode('`, `', array_keys($actionplan)) . '`';
$data = '\'' . implode('\', \'', $actionplan) . '\'';
mysql_query("INSERT INTO `actionplan` ($fields) VALUES ($data)");
}
答案 0 :(得分:1)
您需要在PHP中包含大括号:
<input type="checkbox" name="interests_hobbies[]"...
这将在$ _POST $_POST['interests_hobbies']
中为您提供一个数组。请记住,如果选中该复选框,则只会获得POST值。因此,如果没有选中复选框,则可能为$ _POST ['interests_hobbies']设置空值,因此请确保您的代码处理该值。
<强>更新强> 如果user.php是处理POST的脚本,那么表单操作必须是:
<form action="users.php" method="post">
然后在users.php的顶部,添加此代码以将POST转储到屏幕:
echo "<pre>";var_dump($_POST['interests_hobbies']);echo "</pre>"
(你不需要标签,但我在HTML中使用var_dump时喜欢它们。)
一旦确认您获得了正确的POST值,就可以从那里继续输出或存储值。