php - 使用 PHP 将所有复选框值提交到 MySQL

标签 php mysql

我有一个非常简单的表单,其中包含一些文本字段和 2 个复选框。该表单有效,除非选中两个复选框,只有第一个复选框会添加到数据库中。

到目前为止我有这个:

<?php require_once('connectionsettings.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
. . .

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
    # ------------ edit begins here ------------ #
    if(count($articletags) > 0)
    {
        $articletags_string = implode(",", $articletags);
    }
    # ------------ edit ends here ------------ #

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
    $insertSQL = sprintf("INSERT INTO articles (articletitle, articledescription, articletags) VALUES (%s, %s, %s)",
                   GetSQLValueString($_POST['articletitle'], "text"),
                   GetSQLValueString($_POST['articledescription'], "text"),
                   GetSQLValueString($_POST['articletags[]'], "text")); # --Edited here also

 mysql_select_db($database_MySQL_CSFTDB, $MySQL_CSFTDB);
 $Result1 = mysql_query($insertSQL, $MySQL_CSFTDB) or die(mysql_error());
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
. . .
  <form action="<?php echo $editFormAction; ?>" id="form1" name="form1" method="POST">
  . . .
       <tr>
        <td align="right">&nbsp;</td>
       <td align="left"><p>
          <label> # -- added []s to article name --
          <input type="checkbox" name="articletags[]" value="checkbox" id="articletags_0" />
          Checkbox</label>
        <br />
        <label>
          <input type="checkbox" name="articletags[]" value="checkbox 2" id="articletags_1" />
          Checkbox</label>
         <br />
         <br />
        </p></td>
     </tr>
      . . .
</body>
</html>

最佳答案

  Use checkbox as array so it can store multiple value.

<input type="checkbox" name="articletags[]" value="checkbox" id="articletags_0" />
<input type="checkbox" name="articletags[]" value="checkbox 2" id="articletags_1" />



 $articletags = $_POST['articletags'];

在将值插入数据库之前将数组转换为字符串。

if(count($articletags) > 0)
{
 $articletags_string = implode(",", $articletags);
}

在数据库中插入新值...

关于php - 使用 PHP 将所有复选框值提交到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10911478/

相关文章:

php - 如果条件匹配则应用约束

php - 对连接中的选择结果进行计数

php - 是否可以使用 MySQL 触发器发送电子邮件?

php - 加速mysql查询

php - Wall 脚本的问题

php - SQL 语法错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法

javascript - 带有 PHP 的 wordpress 自定义 html 小部件

php - 登录后如何访问特定的mysql数据行和列?

php - SQL 查询失败

java - 多参数查询