1

I have a PHP form with checkboxes in it; I want to insert a new line item for each checkbox item. Here's my form:

<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>

...and here's my 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++;

I keep getting time-outs and failures of all kinds - what am I doing wrong?

1 Answer 1

1

Try adding parentheses like below 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++;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Simple as that! Thanks, Yogesh - I just couldn't see it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.