php - 将复选框 PHP 表单项作为数组插入到 MySQL 数据库

标签 php mysql arrays checkbox

我有一个带有复选框的 PHP 表单;我想为每个复选框项目插入一个新的行项目。这是我的表格:

<form id="form1" name="form1" method="post" action="/form/formsubmit.php?">
  <p>&nbsp;</p>
  <table align="center">
    <tbody><tr valign="baseline">
      <td><p>          <input type="checkbox" value="2" name="Cat_Ref_Array[]">
          <label for="Category">Food</label>
                    <input type="checkbox" value="3" name="Cat_Ref_Array[]">
          <label for="Category">Camping</label>
                    <input type="checkbox" value="5" name="Cat_Ref_Array[]">
          <label for="Category">Shopping</label>
                    <input type="checkbox" value="7" name="Cat_Ref_Array[]">
          <label for="Category">Cinema</label>
                </p></td>
    </tr>
    <tr valign="baseline">
      <td><select name="dist">
        <option value="5">5 Miles</option>
        <option value="10">10 Miles</option>
        <option value="25">25 Miles</option>
        <option value="50">50 Miles</option>
        <option value="100">100 Miles</option>
      </select></td>
    </tr>
    <tr valign="baseline">
      <td><input type="submit" value="Insert record"></td>
    </tr>
  </tbody></table>
  <input type="hidden" value="-1" name="Ref">
  <input type="hidden" value="form1" name="MM_insert">
</form>

...这是我的 PHP

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

 $chkbox = $_POST['Cat_Ref_Array'];
 $i = 0;
 While($i<sizeof($chkbox))
  $insertSQL = sprintf("INSERT INTO locations (`Ref`, Cat_Ref, dist) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['Ref'], "text"),
                       GetSQLValueString($chkbox[$i], "int"),
                       GetSQLValueString($_POST['dist'], "int"));

  mysql_select_db($database_DB, $DB);
  $Result1 = mysql_query($insertSQL, $DB) or die(mysql_error());
  $i++;

我不断遇到各种超时和失败 - 我做错了什么?

最佳答案

尝试像下面这样添加括号 for while

$i = 0;
 While($i<sizeof($chkbox)){
  $insertSQL = sprintf("INSERT INTO locations (`Ref`, Cat_Ref, dist) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['Ref'], "text"),
                       GetSQLValueString($chkbox[$i], "int"),
                       GetSQLValueString($_POST['dist'], "int"));

  mysql_select_db($database_DB, $DB);
  $Result1 = mysql_query($insertSQL, $DB) or die(mysql_error());
  $i++;
}

关于php - 将复选框 PHP 表单项作为数组插入到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32569590/

相关文章:

javascript - 如何设置 jQuery Select2 的选定值?

php - 我如何合并从 Mysql 获取的 HTML 表的行

MySQL DataConnections 未关闭/合并

arrays - 用ssh迭代数组

php - 将数组插入mysql数据库

php - 如何在laravel Blade中显示存储图像?

php - 反向引用在 PHP 中不起作用

php - 自定义 form_open() 添加元素

php - 从 PHP 的下拉列表中过滤搜索结果

javascript - 将数组转换为嵌套对象列表 - Eloquent Javascript 4.3