1

我目前正在创建一个带有复选框的表单。在我让它工作的那一刻,它只是将最后一个选中的复选框添加到数据库表中。我需要它将所有选定的值添加到表中。我想我可能需要使用内爆,但我不确定把它放在哪里。任何帮助将不胜感激。

行动计划.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>

用户.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)");
}
4

1 回答 1

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 值,您就可以从那里继续输出或存储这些值。

于 2013-04-04T21:37:06.003 回答