我正在尝试在 POST 方法中向 PHP 提交多个复选框。每个选定的项目都应该返回一个简单的 SQL 查询,这是我目前的代码:
HTML:
<input type="checkbox" name="synco[]" value="email" checked="checked" /> Sync email
<input type="checkbox" name="synco[]" value="birthday" checked="checked" /> Sync birthday
<input type="checkbox" name="synco[]" value="gender" checked="checked" /> Sync gender
<input type="checkbox" name="synco[]" value="bio" checked="checked" /> Sync bio
<input type="checkbox" name="synco[]" value="website" checked="checked" /> Sync website
PHP:
$synco = $_POST['synco'];
if(empty($synco))
{
// Empty selection Error
}
else
{
foreach($synco as $item) {
if ($synco == birthday) {
$item = $birthday;
$updaterow = birthday;
}
elseif ($synco == email) {
$item = $user_profile[email];
$updaterow = email_address;
}
elseif ($synco == gender) {
$item = $gender;
$updaterow = gender;
}
elseif ($synco == website) {
$item = $user_profile[website];
$updaterow = website;
}
else {
$item = $user_profile[bio];
$updaterow = bio;
}
$mmhclass->db->query("UPDATE `table1` SET `[1]` = '[2]' WHERE `user_id` = '[3]';", array($updaterow, $item, $user_profile['id']));
// JSON Message
}
问题是:当我提交表单时,无论检查多少输入,sql 都只更新 1 项。