使用带有Dreamweaver的php将多个选中的值插入到一个数据库字段中

I'm currently working on a project which requires the user ticking two or more subject checkboxes and then submit to the database but with my current script i'm finding difficulties, it only insert the first checked box with Y, but what i want to achieve is when the user ticks eg. Science and Mathematics, it should be inserted in the database field as Science,Mathematics.

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

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"] == "main_form")) {
  $insertSQL = sprintf("INSERT INTO regis (email, write_exam) VALUES (%s, %s)",
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString(isset($_POST['written_tests']) ? "true" : "", "defined","'Y'","'N'"));

  mysql_select_db($database_sample, $sample);
  $Result1 = mysql_query($insertSQL, $sample) or die(mysql_error());
}

HTML

<form id="subject_form" action="user.php">
<input name="subject" type="checkbox" id="subject" value="Science" />
        Science &nbsp;
        <input name="subject" type="checkbox" id="subject" value="Mathematics" />
        Mathematics&nbsp;
</form>

Edit:

  • You should change form check boxes with name subject to array subject[] containing both 'Science' and 'Mathematics' values. Then change SQL inserg code to store subject[] array elements which are checked, and make

    $selectedSubjects = implode(',', $_POST['subject']);

and insert $selectedSubjects value into DB.

  • Consider changing your HTML to:

<form id="subject_form" action="user.php"> <input name="subject[]" type="checkbox" id="subject" value="Science" /> Science &nbsp; <input name="subject[]" type="checkbox" id="subject" value="Mathematics" /> Mathematics&nbsp; </form>