如何将多个复选框值插入数据库?

How to insert multiple checkbox values to database?

我在 html 表单标签中有以下代码:

<label for="client">Services Included:</label>
    <p><input type="checkbox" name="service[]" value="Spa"> Spa </p>
    <p><input type="checkbox" name="service[]" value="Massage"> Massage </p>
    <p></p>
    <input type="submit" value="Submit" />

在 php 代码中我有这个:

      if(!empty($_POST['service']))
  {
    foreach ($_POST['service'] as $selected) 
    {
      $query7 ="INSERT INTO reservation_includes_service VALUES (3,'".$selected."');";
      $queryexe7 = mysql_query($queryexe7);

    }
  }

虽然我已经尝试了所有方法,但查询不起作用...有什么想法吗? 感谢您的帮助:)

正如我在评论和作为社区 wiki 发布时所说:

"mysql_query($queryexe7) wrong variable. You're just creating some type of loop and is confused as to which variable to use. It's kind of like "monkey in the middle", if I could say.".

这是您需要使用的:

$queryexe7 = mysql_query($query7);

您应该考虑使用 mysqli_ 或 PDO API。 mysql_ 已弃用并从 PHP 7.

中删除