我目前正在开发一个项目,要求用户勾选两个或更多主题复选框,然后提交到数据库但是使用我当前的脚本我遇到了困难,它只插入带有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>
答案 0 :(得分:0)
编辑:
您应该更改表单复选框,其名称取决于包含'Science'和'Mathematics'值的数组subject []。然后更改SQL inserg代码以存储被检查的subject []数组元素,并生成
$selectedSubjects = implode(',', $_POST['subject']);
并将$ selectedSubjects值插入DB。
<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>