所以我想在mysql中插入多个复选框,但无法知道如何。
从数据库中选择并插入:
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO tournamenteams (id, id_tournament, id_team) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['id'], "int"),
GetSQLValueString($_POST['id_tournament'], "int"),
GetSQLValueString($_POST['id_team'], "int"));
mysql_select_db($database_searchon, $searchon);
$Result1 = mysql_query($insertSQL, $searchon) or die(mysql_error());
}
现在我创建了一个表单并在表单内部显示了复选框。我选择数据库记录并在多个复选框上显示它,然后我想要做的是选择我想要的复选框并在我提交表单时将其全部插入,但它只提交我标记的第一个复选框。
形式:
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<select name="id_tournament">
<?php
do {
?>
<option value="<?php echo $row_tournaments['id_tournament']?>" ><?php echo $row_tournaments['id_tournament']?></option>
<?php
} while ($row_tournaments = mysql_fetch_assoc($tournaments));
?>
</select>
<?php do { ?>
<?php echo $row_teams['id_team']?>:<input type="checkbox" name="id_team" value="<?php echo $row_teams['id_team']?>" />
<?php } while ($row_teams = mysql_fetch_assoc($teams)); ?>
<input type="submit" value="Insert record" />
<input type="hidden" name="id" value="" />
<input type="hidden" name="MM_insert" value="form1" />
</form>
复选框:
<?php do { ?>
<?php echo $row_teams['id_team']?>:<input type="checkbox" name="id_team" value="<?php echo $row_teams['id_team']?>" />
<?php } while ($row_teams = mysql_fetch_assoc($teams)); ?>
整个代码以获得更好的视图:
<?php require_once('Connections/searchon.php'); ?>
<?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")) {
$insertSQL = sprintf("INSERT INTO tournamenteams (id, id_tournament, id_team) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['id'], "int"),
GetSQLValueString($_POST['id_tournament'], "int"),
GetSQLValueString($_POST['id_team'], "int"));
mysql_select_db($database_searchon, $searchon);
$Result1 = mysql_query($insertSQL, $searchon) or die(mysql_error());
}
mysql_select_db($database_searchon, $searchon);
$query_tournamenteams = "SELECT * FROM tournamenteams";
$tournamenteams = mysql_query($query_tournamenteams, $searchon) or die(mysql_error());
$row_tournamenteams = mysql_fetch_assoc($tournamenteams);
$totalRows_tournamenteams = mysql_num_rows($tournamenteams);
mysql_select_db($database_searchon, $searchon);
$query_tournaments = "SELECT * FROM tournaments";
$tournaments = mysql_query($query_tournaments, $searchon) or die(mysql_error());
$row_tournaments = mysql_fetch_assoc($tournaments);
$totalRows_tournaments = mysql_num_rows($tournaments);
mysql_select_db($database_searchon, $searchon);
$query_teams = "SELECT * FROM teams";
$teams = mysql_query($query_teams, $searchon) or die(mysql_error());
$row_teams = mysql_fetch_assoc($teams);
$totalRows_teams = mysql_num_rows($teams);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<select name="id_tournament">
<?php
do {
?>
<option value="<?php echo $row_tournaments['id_tournament']?>" ><?php echo $row_tournaments['id_tournament']?></option>
<?php
} while ($row_tournaments = mysql_fetch_assoc($tournaments));
?>
</select>
<?php do { ?>
<?php echo $row_teams['id_team']?>:<input type="checkbox" name="id_team" value="<?php echo $row_teams['id_team']?>" />
<?php } while ($row_teams = mysql_fetch_assoc($teams)); ?>
<input type="submit" value="Insert record" />
<input type="hidden" name="id" value="" />
<input type="hidden" name="MM_insert" value="form1" />
</form>
<p> </p>
<p> </p>
</body>
</html>
<?php
mysql_free_result($tournamenteams);
mysql_free_result($tournaments);
mysql_free_result($teams);
?>
PS:我正在使用Dreamweaver,因为我还在开始编码:)
任何帮助将不胜感激,
Cumps。
答案 0 :(得分:0)
为了让PHP接收每个复选框值(在数组中,自然而然)是将[]
放在组名的末尾。因此,您创建复选框的部分应更改为:
<?php do { ?>
<?php echo $row_teams['id_team']?>:<input type="checkbox" name="id_team[]" value="<?php echo $row_teams['id_team']?>" />
<?php } while ($row_teams = mysql_fetch_assoc($teams)); ?>
请注意,唯一的更改是name="id_team[]"
。