0

我正在尝试在 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 项。

4

2 回答 2

1

代替

if ($synco == birthday) {

尝试这个

if ($item == birthday) {

对于每个 $synco,您都处于 foreach 循环中,它被假定为 $item,因此您只需要检查 $item

于 2013-06-10T12:54:01.373 回答
1
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;
  }

您应该使用$item而不是$synco更多。如果您在以下名称下有预定义的常量:生日,电子邮件,通用,网站......然后忽略这个:

你应该用引号括起来

例如:

if ($item == "birthday"){

}

尝试为您的查询使用另一种样式:

原来的:

  $mmhclass->db->query("UPDATE `table1` SET `[1]` = '[2]' WHERE `user_id` = '[3]';", array($updaterow, $item, $user_profile['id']));

我会接近的方式:

$mmhclass->db->query("UPDATE `table1` SET `".$updaterow."`= '".$item."' WHERE `user_id`= '".$user_profile['id']."'");

或者使用准备好的语句:

$Query = $mmhclass->db->prepare("UPDATE `table1` SET ?=? WHERE user_id=?");
$Query->bind_param('ssi',$updaterow,$item,$user_profile['id']);
$Query->execute();
$Query->close();
于 2013-06-10T12:57:52.280 回答