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

时间:2015-09-14 16:30:10

标签: 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++;

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

1 个答案:

答案 0 :(得分:1)

尝试添加如下所示的括号

$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++;
}