php - 使用 Dreamweaver 将多个检查值插入到一个数据库字段中

标签 php mysql dreamweaver

我目前正在开发一个项目,该项目需要用户勾选两个或多个主题复选框,然后提交到数据库,但使用我当前的脚本,我发现困难,它只插入第一个带有 Y 的复选框,但是什么我想要实现的是当用户勾选时,例如。科学和数学,应在数据库字段中插入为 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>

最佳答案

编辑:

  • 您应该更改名称受包含“科学”和“数学”值的数组 subject[] 约束的表单复选框。然后更改SQL inserg代码以存储检查到的subject[]数组元素,并make

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

并将 $selectedSubjects 值插入数据库。

  • 考虑将 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>

关于php - 使用 Dreamweaver 将多个检查值插入到一个数据库字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37455344/

相关文章:

php - upsert 时出现重复键错误 [说明]

php - 使用PHP根据单选按钮选择插入图像MYSQL

php - 模拟正在测试的同一类中的方法

mysql - 复杂的 MySQL 命令出现问题。我可以做这样的复合陈述吗?

mysql - 使用连接在单行中获取多个值

php - 同一张表上的更新

php - 返回 SQL 语句的 MySQL 语句?

html - Dreamweaver spry 小部件兼容性

css - 如何在 Dreamweaver CS4 的 3 列液体、页眉和页脚中扩展侧边栏

html - 如何让站点根据用户的浏览器窗口自动缩放