我目前正在开发一个项目,该项目需要用户勾选两个或多个主题复选框,然后提交到数据库,但使用我当前的脚本,我发现困难,它只插入第一个带有 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
<input name="subject" type="checkbox" id="subject" value="Mathematics" />
Mathematics
</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
<input name="subject[]" type="checkbox" id="subject" value="Mathematics" />
Mathematics
</form>
关于php - 使用 Dreamweaver 将多个检查值插入到一个数据库字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37455344/